Esempio n. 1
0
int main  (int argc, char *argv[]) {
	char buf[BUFSIZE];
	pid_t childpid = 0; 
	int fd;
	int i, n;

	if (argc != 4){       /* check for valid number of command-line arguments */
		fprintf (stderr, "Usage: %s processes log_file atomic_file\n", argv[0]);
		return 1;
	}     

	n = atoi(argv[1]);                              /* create a process chain */
	for (i = 1; i < n; i++)
		if ( (childpid = fork()) )
			break;
	if (childpid == -1) {
		perror("Failed to fork");
		return 1;
	}     


	/* open log file after the fork */
	fd = open(argv[2], CREATE_FLAGS, CREATE_PERMS);
	if (fd < 0) {
		perror("Failed to open file");
		return 1;
	}

	/* open atomic log after fork */
	if (atomic_log_open(argv[3]) == -1) {
		fprintf(stderr, "Failed to open log file");
		return 1;
	}

	/* write twice to the common log file */
	sprintf(buf, "i:%d process:%ld ", i, (long)getpid());
	write(fd, buf, strlen(buf));
	atomic_log_string(buf);

	sprintf(buf, "parent:%ld child:%ld\n", (long)getppid(), (long)childpid);
	write(fd, buf, strlen(buf));
	atomic_log_string(buf);

	/* atomic_log_printf("i:%d process:%ld parent:%ld child:%ld\n", i, (long)getpid(), (long)getppid(), (long)childpid); */
	if ( atomic_log_send() == -1 ) {
		fprintf(stderr, "Failed to send to log file");
		return 1;
	}
	atomic_log_close();

	return 0;
}
Esempio n. 2
0
main(int argc, char* argv[]){

	char *LogFileName; // pathname
	int RandSeed = 1; // rand seed
	int ChildProcessIndex = 0;
	int NumberofChildProcess = 0; // number of process
	int NumberofMessage = 1;
	double SleepForAWhile = 0;
	int fd;
	
	if(argc != 4)
		err_quit("usage: hw1 filename seed numberofprocess\n");
	
	pid_t pid;
	LogFileName = argv[1];
	RandSeed = atoi(argv[2]);
	NumberofChildProcess = atoi(argv[3]);
		
	//Set the seed for random number generator
	srand(RandSeed);


	//At most fork NumberofPorcess childs, and only parent can fork.
	while(ChildProcessIndex < NumberofChildProcess){
		// At most 200 messages
		NumberofMessage = (int)100 * (1.0 + (double)rand()/(RAND_MAX));
		
		// At most sleep for 1 seconds
		SleepForAWhile = (double)rand()/(RAND_MAX);
		
		printf("Number of messages for Child Process %c = %d\n", 
					 ChildProcessIndex + 65, NumberofMessage); 
		printf("sleep_time for Child Process %c = %f\n", 
					 ChildProcessIndex + 65, SleepForAWhile); 
		fflush(stdout);

		// Fork a child process
		if ((pid=fork()) < 0)
			/*Fork error*/
			err_quit("fork error\n");	
		else if ( pid == 0){
			// Child process
			//
			// We will ask the child process to sleep 
			// for a while first. Then, it will generate
			// a number of log messages, store the messages
			// in memory, and send the messages to the log
			// file at the end. 
			int index = 0;
			
			usleep(SleepForAWhile * 1000000);

			if((fd = atomic_log_open( LogFileName )) == -1)
				err_quit("open/create file error\n");

			for (index = 0; index < NumberofMessage; index++){
				char LogMessage[40];
				
				sprintf(LogMessage, "Child Process %c: Message-ID: %d \n", 
					ChildProcessIndex + 65, index);
				
				#ifdef DEBUG
				fprintf(stderr, LogMessage);
				#endif
				if(log_string(LogMessage) < strlen(LogMessage)) {
					break;
				}
			}
			atomic_log_send(fd);
			_exit(0);
		}else
			/*Parent Process*/
			ChildProcessIndex++;

	}
  
	/*Fork successfully*/
	exit(0);
	
}