Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have a look at this <a href="http://www.cs.nmsu.edu/~jcook/Tools/pthreads/pthreads.html" rel="nofollow">http://www.cs.nmsu.edu/~jcook/Tools/pthreads/pthreads.html</a></p> <p>You propably know that a fork() produces a copy of the calling process and that this copy is (almost) identical to the original - they only differ in that fork() in the child = 1</p> <p>Try (and tell me if its working, i could not test it :)</p> <pre><code> for(i=1; i&lt;=counter; i++){ child_pid = fork(); switch(child_pid) { case -1: printf("Error occured with fork()\n"); exit(1); case 0: print_screen(i); /* Child Process */ exit(0); default: printf("...\n"); } } </code></pre> <p>EDIT: works for me</p> <p>EDIT: okay i did some testing, should rather use <code>if</code> instead of <code>case</code> (also added dirext output of file content - should remove that if file size growth ); now thats what i had as code: </p> <pre><code>#include &lt;stdio.h&gt; /* basic I/O routines. */ #include &lt;unistd.h&gt; /* define fork(), etc. */ #include &lt;sys/types.h&gt; /* define pid_t, etc. */ #include &lt;sys/wait.h&gt; /* define wait(), etc. */ #include &lt;signal.h&gt; /* define signal(), etc. */ #include &lt;pthread.h&gt; #include &lt;time.h&gt; void print_screen(int i); int counter=0; int main(int argc, char* argv[]) { FILE* fptr; fptr = fopen("sample.txt","w"); int counter = atoi(argv[1]); int i,k; int temp; pid_t child_pid; int child_status; char array[counter]; srand ( time(NULL) ); for(i=0; i&lt;counter; i++){ temp = rand()%4; if( temp==0 ) { fprintf(fptr,"A\n"); printf("A\n"); array[i]='A'; } else if( temp==1 ) { fprintf(fptr,"C\n"); printf("C\n"); array[i]='C'; } else if( temp==2 ) { fprintf(fptr,"G\n"); printf("G\n"); array[i]='G'; } else if( temp==3 ) { fprintf(fptr,"T\n"); printf("T\n"); array[i]='T'; } } fclose(fptr); for(i=1; i&lt;=counter; i++){ child_pid = fork(); if (child_pid == -1){ printf("Error occured with fork()\n"); exit(-1); } else if (child_pid == 0){ print_screen(i); exit(0); } else{ printf("something \n"); } } wait(&amp;child_status); execl("/usr/bin/killall","killall","tail",(char *) 0); return 0; } void print_screen(int i){ char* str; FILE* fptr; fptr=fopen("sample.txt","r"); fseek(fptr,i,SEEK_SET); fscanf(fptr,"%s",str); printf("Process Number %d, Character = %s\n",i,str); sleep(1); fclose(fptr); return; } </code></pre> <p>it doesnt work correct (i'm pretty sure there is some kind of collision when the pthreads are reading from the file at the same time) but at least you will have something to continue working with. </p> <p>I got this output:</p> <p>A</p> <p>T</p> <p>A</p> <p>T</p> <p>something </p> <p>something </p> <p>something </p> <p>something </p> <p>Process Number 4, Character = A</p> <p>Process Number 3, Character = A</p> <p>Process Number 2, Character = T</p> <p>Process Number 1, Character = T</p> <p>tail: no process found</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload