C program sleepingStudent.c Write a program with
C programs
sleepingStudent.c (30 Points)Write a program with the following infinite loop in main():
canSleep = 1;
while (canSleep)
{
printf("Student: "(snore)"n");
sleep(1);
}
It waits for global var canSleep to be set to 0. A signal handler function for SIGUSR1 should set canSleep to 0. After the loop ends have it print:
"Student: "Okay, okay. I'll wake up!"n"
and return EXIT_SUCCESS to the OS.
Test it like so (the ampersand says "run it in the background"):
$ ./sleepingStudent &
[1] 28684
$ Student: "(snore)"
Student: "(snore)"
Student: "(snore)"
Student: "(snore)"
Student: "(snore)"
kiStudent: "(snore)"
ll Student: "(snore)"
Student: "(snore)"
-10 Student: "(snore)"
Student: "(snore)"
28Student: "(snore)"
Student: "(snore)"
68Student: "(snore)"
4Student: "(snore)"
Oh no! It has an infinite loop and runs in the background! How do I stop it?!?
Oh yeah, SIGUSR1 (integer 10). What is the process id? Oh yeah, it told us (in this case 28684).
$ kill -10 28684
Note: While you're typing, the student is still sleeping. Just ignore it and keep typing.
$ Student: "Okay, okay. I'll wake up!"
$
alarmClock.c (30 points)
Write a program that takes 2 command line parameters: a sibling pid and a number of seconds.
If the sibling pid is not a positive integer it should print "Illegal pid.n" and quit returning EXIT_FAILURE.
If the number of seconds is not a positive integer it should print "Illegal number of seconds.n" and quit returning EXIT_FAILURE.
Then it should count down the number of seconds down to zero. Have a loop that prints how many seconds remain, and then does sleep(1) until it reaches 0.
When the time is up it should:
Print "Clock "Ding! Ding! Ding!"n"
Send the SIGUSR1 signal to sibling process.
Quit and return EXIT_SUCCESS to the OS.
Hey! Now we have a more elegant way of stopping the student! (Again, you'll have to type over the sleeping student. In this case I typed ./alarmClock 3216 30 &.)
$ ./sleepingStudent &
[2] 3216
$ Student: "(snore)"
.Student: "(snore)"
/aStudent: "(snore)"
larmClock Student: "(snore)"
Student: "(snore)"
Student: "(snore)"
Student: "(snore)"
Student: "(snore)"
3Student: "(snore)"
21Student: "(snore)"
6Student: "(snore)"
Student: "(snore)"
Student: "(snore)"
30Student: "(snore)"
&
[3] 3222
[instructor@JoesLaptopFedora16 Assign2]$ Clock "00:00:30"
Student: "(snore)"
Clock "00:00:29"
Student: "(snore)"
Clock "00:00:28"
Student: "(snore)"
Clock "00:00:27"
Student: "(snore)"
Clock "00:00:26"
Student: "(snore)"
Clock "00:00:25"
Student: "(snore)"
Clock "00:00:24"
Student: "(snore)"
Clock "00:00:23"
Student: "(snore)"
Clock "00:00:22"
Student: "(snore)"
Clock "00:00:21"
Student: "(snore)"
Clock "00:00:20"
Student: "(snore)"
Clock "00:00:19"
Student: "(snore)"
Clock "00:00:18"
Student: "(snore)"
Clock "00:00:17"
Student: "(snore)"
Clock "00:00:16"
Student: "(snore)"
Clock "00:00:15"
Student: "(snore)"
Clock "00:00:14"
Student: "(snore)"
Clock "00:00:13"
Student: "(snore)"
Clock "00:00:12"
Student: "(snore)"
Clock "00:00:11"
Student: "(snore)"
Clock "00:00:10"
Student: "(snore)"
Clock "00:00:09"
Student: "(snore)"
Clock "00:00:08"
Student: "(snore)"
Clock "00:00:07"
Student: "(snore)"
Clock "00:00:06"
Student: "(snore)"
Clock "00:00:05"
Student: "(snore)"
Clock "00:00:04"
Student: "(snore)"
Clock "00:00:03"
Student: "(snore)"
Clock "00:00:02"
Student: "(snore)"
Clock "00:00:01"
Student: "(snore)"
Clock "Ding! Ding! Ding!"
Student: "Okay, okay. I'll wake up!"
[2]- Done ./sleepingStudent
[3]+ Done ./alarmClock 3216 30
$
launcher.c (40 Points)
Write a program to launch both alarmClock and sleepingStudent.
It should:
Ask for the number of seconds to the student should be allowed to sleep (from 1 to 3600)
fork() a process to run sleepingStudent. Keep the pid returned by fork().
fork() a process to run alarmClock. This one takes two numeric parameters that have to be turned into strings. Use this code as a guide:
#define MAX_LINE 256
#define ALARMCLOCK_PROG "alarmClock"
. . .
char pid[MAX_LINE];
char secs[MAX_LINE];
snprintf(pid,MAX_LINE,"%d",studentPid);
snprintf(secs,MAX_LINE,"%d",numSecs);
execl(ALARMCLOCK_PROG,ALARMCLOCK_PROG,pid,secs,NULL);
wait() for both processes to finish.
Prints "Such a good student!"
Return EXIT_SUCCESS to the OS.
$ ./launcher
How many seconds shall the student be allowed to sleep? 20
Student: "(snore)"
Student: "(snore)"
Clock "00:00:20"
Student: "(snore)"
Clock "00:00:19"
Student: "(snore)"
Clock "00:00:18"
Student: "(snore)"
Clock "00:00:17"
Student: "(snore)"
Clock "00:00:16"
Student: "(snore)"
Clock "00:00:15"
Student: "(snore)"
Clock "00:00:14"
Student: "(snore)"
Clock "00:00:13"
Student: "(snore)"
Clock "00:00:12"
Student: "(snore)"
Clock "00:00:11"
Clock "00:00:10"
Student: "(snore)"
Clock "00:00:09"
Student: "(snore)"
Student: "(snore)"
Clock "00:00:08"
Clock "00:00:07"
Student: "(snore)"
Clock "00:00:06"
Student: "(snore)"
Student: "(snore)"
Clock "00:00:05"
Student: "(snore)"
Clock "00:00:04"
Student: "(snore)"
Clock "00:00:03"
Clock "00:00:02"
Student: "(snore)"
Clock "00:00:01"
Student: "(snore)"
Student: "(snore)"
Clock "Ding! Ding! Ding!"
Student: "Okay, okay. I'll wake up!"
Such a good student!
$
No Comment to " C program sleepingStudent.c Write a program with "