lab2 - for(i = 0 i<= atoi(argv[1 i if(s.ReadValue(0 ==...

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 <= atoi(argv[1]); i++) { if(s.ReadValue(0) == 0)//user enters 0,child will //generate random #first { cout <<" Child " << getpid() <<endl; cout << rand() << endl; } else //Parent will create random # first { cout <<" Parent " << getpid() <<endl; c cout << rand() << 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 << gettimeofday(0, 0) << endl; c } else { cout <<"Number inputted is greater then 0 or 1" <<endl; } } //Destroys all semaphores s.Destroy(); return 0; }...
View Full Document

This note was uploaded on 11/30/2009 for the course CS 360 taught by Professor Hippler during the Spring '09 term at Bowling Green.

Page1 / 2

lab2 - for(i = 0 i<= atoi(argv[1 i if(s.ReadValue(0 ==...

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