Create a C program using threads: Create two child threads (from the main, initi
ID: 3908540 • Letter: C
Question
Create a C program using threads: Create two child threads (from the main, initial thread), then busywaits until all child threads have completed and then perform a clean exit.1 of the child threads will monitor the countdown timer, upon reaching the specified number of seconds it will notify (using either signal(), or pipe() ) the other child thread (i.e. its sibling thread) to terminate, and then terminate itself.
The other child process will implement a “clock” which prints the hour, minute, and second of the time of day to the user terminal once every second (in human readable form, hint: use localtime). When the hour, minute, and second matches the user’s provided countdown time, an alarm would be printed in addition to the time let the user know the program is terminating.
After all child threads have terminated, the parent thread (and the process) shall print a friendly message, then perform a clean exit. Examples for the command line usage: [user@...]$program 33 --- the program to run for 33 seconds [user@...]$program (no arguments) --- the program would run for 25 seconds(default value if no argument is passed) [user@...]$program 188 --- the program would run for 3minutes, 8 seconds
Create a C program using threads: Create two child threads (from the main, initial thread), then busywaits until all child threads have completed and then perform a clean exit.
1 of the child threads will monitor the countdown timer, upon reaching the specified number of seconds it will notify (using either signal(), or pipe() ) the other child thread (i.e. its sibling thread) to terminate, and then terminate itself.
The other child process will implement a “clock” which prints the hour, minute, and second of the time of day to the user terminal once every second (in human readable form, hint: use localtime). When the hour, minute, and second matches the user’s provided countdown time, an alarm would be printed in addition to the time let the user know the program is terminating.
After all child threads have terminated, the parent thread (and the process) shall print a friendly message, then perform a clean exit. Examples for the command line usage: [user@...]$program 33 --- the program to run for 33 seconds [user@...]$program (no arguments) --- the program would run for 25 seconds(default value if no argument is passed) [user@...]$program 188 --- the program would run for 3minutes, 8 seconds
Create a C program using threads: Create two child threads (from the main, initial thread), then busywaits until all child threads have completed and then perform a clean exit.
1 of the child threads will monitor the countdown timer, upon reaching the specified number of seconds it will notify (using either signal(), or pipe() ) the other child thread (i.e. its sibling thread) to terminate, and then terminate itself.
The other child process will implement a “clock” which prints the hour, minute, and second of the time of day to the user terminal once every second (in human readable form, hint: use localtime). When the hour, minute, and second matches the user’s provided countdown time, an alarm would be printed in addition to the time let the user know the program is terminating.
After all child threads have terminated, the parent thread (and the process) shall print a friendly message, then perform a clean exit. Examples for the command line usage: [user@...]$program 33 --- the program to run for 33 seconds [user@...]$program (no arguments) --- the program would run for 25 seconds(default value if no argument is passed) [user@...]$program 188 --- the program would run for 3minutes, 8 seconds
Explanation / Answer
here is your program : ------------->>>>>>>>>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
/* Read characters from the pipe and echo them to stdout. */
int read_from_pipe (int file)
{
FILE *stream;
int c;
stream = fdopen (file, "r");
c = fgetc (stream);
return c;
}
/* Write some random text to the pipe. */
int write_to_pipe (int file)
{
FILE *stream;
static int n = 0;
n++;
stream = fdopen (file, "w");
fprintf (stream, "%d",n);
fclose (stream);
return n;
}
int clockTick(int w){
pid_t pid,wpid;
int status = 0;
int mypipe[2];
/* Create the pipe. */
if (pipe(mypipe))
{
fprintf (stderr, "Pipe failed. ");
return EXIT_FAILURE;
}
/* Create the child process. */
pid = fork ();
if (pid == (pid_t) 0)
{
/* This is the child process.
Close other end first. */
close (mypipe[0]);
int n = 0;
while(n < w){
n = write_to_pipe(mypipe[1]);
sleep(1);
}
}
else if (pid < (pid_t) 0)
{
/* The fork failed. */
fprintf (stderr, "Fork failed. ");
return EXIT_FAILURE;
}
else
{
/* This is the parent process.
Close other end first. */
pid = fork();
if (pid == (pid_t) 0)
{
/* This is the child process.
Close other end first. */
close (mypipe[1]);
int n = 0;
time_t timer;
char buffer[26];
struct tm* tm_info;
while(n < w){
//clear();
n = read_from_pipe (mypipe[0]);
time(&timer);
tm_info = localtime(&timer);
strftime(buffer, 26, "%H:%M:%S", tm_info);
puts(buffer);
sleep(1);
}
}
else if (pid < (pid_t) 0)
{
/* The fork failed. */
fprintf (stderr, "Fork failed. ");
return EXIT_FAILURE;
}
else
{
/* This is the parent process.
Close other end first. */
while ((wpid = wait(&status)) > 0);
return EXIT_SUCCESS;
}
}
}
int main ()
{
int i = clockTick(33);
if(i == EXIT_FAILURE){
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}