CS604 Idea Solution
Page 1 of 1 • Share
CS604 Idea Solution
for q=2
take an idea........
take an idea........
- Code:
#include <stdio.h> /* printf, stderr, fprintf */
#include <sys/types.h> /* pid_t */
#include <unistd.h> /* _exit, fork */
#include <stdlib.h> /* exit */
#include <errno.h> /* errno */
int main(void)
{
pid_t pid;
/* Output from both the child and the parent process
* will be written to the standard output,
* as they both run at the same time.
*/
pid = fork();
if (pid == -1)
{
/* Error:
* When fork() returns -1, an error happened
* (for example, number of processes reached the limit).
*/
fprintf(stderr, "can't fork, error %d\n", errno);
exit(EXIT_FAILURE);
}
if (pid == 0)
{
/* Child process:
* When fork() returns 0, we are in
* the child process.
* Here we count up to ten, one each second.
*/
int j;
for (j = 0; j < 10; j++)
{
printf("child: %d\n", j);
sleep(1);
}
_exit(0); /* Note that we do not use exit() */
}
else
{
/* Parent process:
* When fork() returns a positive number, we are in the parent process
* (the fork return value is the PID of the newly created child process).
* Again we count up to ten.
*/
int i;
for (i = 0; i < 10; i++)
{
printf("parent: %d\n", i);
sleep(1);
}
exit(0);
}
return 0;
}
plhr60- Monstars
-
Posts : 536
Join date : 2011-10-20
Age : 34
Character sheet
Experience:
(33/500)
Similar topics
» CS604 Operating Systems Assignment No. 01 Solution
» ISL201 GDB Idea Solution
» ACC311 3rd GDB idea solution
» MGT510 GDB Idea Solution
» FIN623 Assignment Idea Solution
» ISL201 GDB Idea Solution
» ACC311 3rd GDB idea solution
» MGT510 GDB Idea Solution
» FIN623 Assignment Idea Solution
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Yesterday at 12:21 pm by ali001
» Hemangiom'App
Tue Nov 05, 2024 11:25 am by ali001
» MindfulMe - Mental Health App
Mon Nov 04, 2024 10:50 am by ali001
» Learn Candlestick Patterns
Tue Oct 15, 2024 5:51 am by ali001
» Woh Pagal Si Episode 52 to 62 - Top Pakistani Drama
Sat Sep 21, 2024 6:26 pm by Mir Emmad Ali Khan Domki
» Nearu - share your socials
Sat Sep 21, 2024 1:12 pm by ali001
» Nightclub Tycoon: Idle Empire
Thu Sep 19, 2024 9:16 pm by ali001
» Carnivore - Meat Diet Recipes
Wed Sep 18, 2024 2:37 pm by ali001
» Eid Milad un Nabi Mubarak 2024 (Rabiʻ I 14, 1446 AH)
Tue Sep 17, 2024 3:44 pm by Mir Emmad Ali Khan Domki