lab2 - for (i = 0; i <= atoi(argv[1]); i++) {...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include "/home/cs/kresman/cs327/semaphore.h" int main(int argc,char* argv[]) { //Variable Declaration int pid; int i = 0; i //Creates a Semaphore array Semaphore s(2, 1); S //Verifies there is only 3 arguements in the command line if(argc == 3) { //Verifies that the 2nd arguement is either 1 or 2 and itisnt less then that { //Gives a value to the semaphore array s.Init(0, atoi(argv[2])); s //Retrieves the time of day cout << gettimeofday(0, 0) << endl; c pid = fork(); p //Controls how many random #'s will be created
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: for (i = 0; i &lt;= atoi(argv[1]); i++) { if(s.ReadValue(0) == 0)//user enters 0,child will //generate random #first { cout &lt;&lt;&quot; Child &quot; &lt;&lt; getpid() &lt;&lt;endl; cout &lt;&lt; rand() &lt;&lt; endl; } else //Parent will create random # first { cout &lt;&lt;&quot; Parent &quot; &lt;&lt; getpid() &lt;&lt;endl; c cout &lt;&lt; rand() &lt;&lt; endl; } //This controls the semaphore //if the value is 0 then increment it //to 1, else decrement it to 0 if(s.ReadValue(0) == 0) { s.Signal(0); } else { s.Wait(0); } } } cout &lt;&lt; gettimeofday(0, 0) &lt;&lt; endl; c } else { cout &lt;&lt;&quot;Number inputted is greater then 0 or 1&quot; &lt;&lt;endl; } } //Destroys all semaphores s.Destroy(); return 0; }...
View Full Document

Page1 / 2

lab2 - for (i = 0; i &amp;amp;lt;= atoi(argv[1]); i++) {...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online