fork() problem

i'm just trying to make 2 process read from the same 1 line a time. For some reason only the child reads.

#include<stdio.h>
#include  <sys/types.h>
void getlinefromfilep(void);
void getlinefromfilec(void);
int see=0;
FILE * fileptr1;

//need globe variable to tell pro3 to stop
main()
{
char temp1[100];
char temp2[100];


pid_t  pid;

fileptr1 = fopen("sample.txt","rt");
        if( fileptr1 != NULL )
        {
                printf("opened\n");
		pid = fork();
			if (pid == 0)
			{
				while(see==0)
			        {
					printf("child started\n");
					fscanf(fileptr1,"%s",temp1);
					printf("%s",temp1);
					printf("\n");
					printf("child done\n");
					see=feof(fileptr1);
					printf("see = ");
					printf("%i",see);
					printf("\n");	      
                		} 
			}
			else
			{ 
				int i=0;
				char temp2[100];
				while(see==0)
	        		{
					printf("parnet started\n");
					fscanf(fileptr1,"%s",temp2);
					printf("%s",temp2);
					printf("\n");
					printf("parent done\n");
					see=feof(fileptr1);
					printf("see = ");
					printf("%i",see);
					printf("\n");      
                		}
			}

        }
        else
        {
                printf("not opened");
        }
	fclose(fileptr1);

}

Got it! :slight_smile:

Once a new process is forked unless and untill parent / child synchronization is implemented in the code, there is no way to determine with the logic of code which would run first.

In the code that is being discussed, there is no such parent / child synchronization mechanism hence it is purely the discretion of the kernel to decide which one to run first ( had vfork been used, child is guaranteed to run first), though in most of the cases child runs first before the parent is executed.
Moreover the child and parent are using the common file pointer and before the parent could read the contents of the file child reads and moves the file pointer to read EOF. Thats why there is no file content being displayed on behalf of the parent process.

That could be checked as follows in a simple way,

In the parent modify the parent fscanf - code to print the return value, in most of the cases it would return -1 indicating before parent could start child had completed the processing and hence a '-1' being returned to that.

printf("the return value for parent is %d\n", fscanf(fileptr1,"%s",temp2));

To specifically make a parent to read and child to wait before it could start, here is a weak way of doing it :slight_smile:

in child code

sleep(10);

(or)

have the file being read as a real huge file, then by default time slicing chances are there that parent gets a chance to read from the file before it encounters EOF

(Hope so, the sample file you had used is relatively small)

:slight_smile: :slight_smile:

Parent -child process is not handled properly ..you need to use the WAIT command to handle it..

Sample code Attached ..Hope this may help you

             fork_id=fork\(\);
             if\(fork_id &lt; 0\)\{printf\("fork failed"\);exit\(-1\);\}
             if\(fork_id==0\)
	   \{
	   signal\(SIGSEGV,sig\);
	   if\(\(exec_string=\(char*\) malloc\(32\)\)==NULL\)\{printf\("Cannot allocate memory to exec_string\\n"\);\}
               sprintf\(exec_string,"%s %d %d",argv[2],startproc\_no,endproc_no\);
               system\(exec_string\);
           free\(exec_string\);
               exit\(2\);
    	   \}

            else
             \{
          childpid[counter]=fork_id;
             \}

     \}
                for\(counter=1;counter&lt;=instance_no;counter\+\+\)
	    \{
            wait\(\(int*\)childpid[counter]\);
                 \}

Even if memory allocation fails, processing continuous with unallocated exec_string chunk used in sprintf.

This should dump core.

Just a thought :slight_smile: