4 Replies - 1973 Views - Last Post: 08 October 2012 - 06:49 AM Rate Topic: -----

#1 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 01-September 11

Substituting Semaphores for Pipes

Posted 07 October 2012 - 04:11 PM

I wrote a program that creates a parent and a child. Each time a process generates a random number and cOuts it.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

int main( int argc, char **argv) {
	pid_t           pid;

        hrtime_t  startTime, endTime;
        int numberOfRandoms, whoGoesFirst;
	int             PA[2];  // pipe A
	int             PB[2];  // pipe B
	char            msg[80]; // buffer to hold message
	pipe(PA);  // create the IPC pipe
	pipe(PB);  // create the IPC pipe
	/* fork a child process */
        if ( argc < 2) {
            printf(" usage: a.out #randNums whofirst 0=Parent, 1=child\n");
            exit(1);
         }
        numberOfRandoms = atoi( argv[1] );
        whoGoesFirst = atoi( argv[2] );
        startTime = gethrtime();
	pid = fork();

	if (pid == 0) {		/* child process */
               startTime = gethrtime();
               pid = getpid();
		printf("---->Child: Hello pid: %d \n", pid);
                write(PB[1], "GO",3);
                read(PA[0], msg,3);
                if ( whoGoesFirst==0) { // parent 
                   for ( int i=1; i <= numberOfRandoms; i++) {
                       read(PA[0], msg, 3); // wait for "GO"
                       printf(" Child: number is %d \n", rand() );
                       write(PB[1], "GO",3);  // send " Your turn "
                   }
		}
                else { 
                   for ( int i=1; i <= numberOfRandoms; i++) {
                       printf(" Child: number is %d \n", rand() );
                       write(PB[1], "GO",3);  // send "Your turn"
                       read(PA[0], msg, 3);  // wait for "GO"
                   }
		}
	        endTime = gethrtime();
		printf("Child is done start: %lld   end: %lld\n",startTime,endTime);
                write(PB[1], "GO",3);

	} else {		/* parent process */
		pid = getpid();
		printf("----->Parent: Hello pid: %d \n", pid);
                write(PA[1], "GO",3);
                read(PB[0], msg,3);
		if ( whoGoesFirst==1) { // parent
                   for ( int i=1; i <= numberOfRandoms; i++) {
                       read(PB[0], msg, 3);
                       printf(" Parent: number is %d \n", rand() );
                       write(PA[1], "GO",3);
                   }
                }
                else { 
                   for ( int i=1; i <= numberOfRandoms; i++) {
                       printf(" Parent: number is %d \n", rand() );
                       write(PA[1], "GO",3);
                       read(PB[0], msg, 3);
                   }
                }
		endTime = gethrtime();
                printf("Parent is done start: %lld   end: %lld\n",startTime,endTime);
                write(PA[1],"GO",3);
          }
}




The challenge is now to use semaphores to interleave the outputs and share generated data with the other process and the last process destroys the semaphores. What would be the best way of attacking this? Obviously the topic because discussed now in class is semaphores so I have no prior experience with them.

Thanks

This post has been edited by skatingrocker17: 08 October 2012 - 06:27 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Substituting Semaphores for Pipes

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Substituting Semaphores for Pipes

Posted 07 October 2012 - 10:28 PM

> Substituting Semaphores for Pipes
Your code isn't even using pipes to provide any kind of interleaving - it's just a big free for all at the moment.

> Obviously the topic because discussed now in class is semaphores so I have no prior experience with them.
Perhaps wait until the end of the class, when you'll be more informed.
Was This Post Helpful? 0
  • +
  • -

#3 skatingrocker17  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 117
  • Joined: 01-September 11

Re: Substituting Semaphores for Pipes

Posted 08 October 2012 - 06:26 AM

Sorry, the right code as been corrected in the OP.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: Substituting Semaphores for Pipes

Posted 08 October 2012 - 06:35 AM

Please don't "correct" your post to remove and replace code. This makes following the conservations impossible. Just create a new post in the topic add the new code to this new post and restate the problems. Make the questions as specific as possible.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Substituting Semaphores for Pipes

Posted 08 October 2012 - 06:49 AM

http://linux.die.net/man/2/semop
Scroll down to the "see also" section for further reading.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1