0% found this document useful (0 votes)
17 views5 pages

R51_P3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

R51_P3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PRACTICAL 03

Name : Prathamesh Ratthe Roll no. : C3_51

Aim :- To demonstrate process system calls.

1. CODE :- // parent.c: the parent program

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main (int argc, char **argv)
{
int i = 0;
long sum;
int pid;
int status, ret;
char *myargs [] = { NULL };
char *myenv [] = { NULL };
printf ("Parent: Hello, World!\n");
pid = fork ();
if (pid == 0) {
// I am the child
execve ("child", myargs, myenv);
}
// I am the parent
printf ("Parent: Waiting for Child to complete.\n");
if ((ret = waitpid (pid, &status, 0)) == -1)
printf ("parent:error\n");
if (ret == pid)
printf ("Parent: Child process waited for.\n");
}

Output :
2. CODE :- // child.c: the child program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define A 500
#define B 600
#define C 700
int main (int argc, char **argv)

{
int i, j;
long sum;
// Some arbitrary work done by the child
printf ("Child: Hello, World!\n");
for (j = 0; j < 30; j++ ) {
for (i =0; i < 900000; i++) {
sum = A * i + B * i * i + C;
sum %= 543;
}
}
printf ("Child: Work completed!\n");
printf ("Child: Bye now.\n");
exit (0);
}

Output :

3. CODE :- Fork System Call:(Example 1)

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t pid;
int count = 0;
pid = fork(); // create a child process
if (pid == 0) { // child process
printf("Child process is running.\n");
} else if (pid > 0) { // parent process
printf("Parent process is running.\n");
wait(NULL); // wait for child to complete
printf("Child process has completed.\n");
} else { // fork failed
printf("Fork failed.\n");
return 1;
}
return 0;
}

Output :

4. CODE :-(Example 2)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid;
int value = 0;
pid = fork(); // create a new process
if (pid == -1) {
printf("Fork failed\n");
exit(1);
} else if (pid == 0) {
printf("I am the child process. My pid is %d\n", getpid());
value = 1;
} else {
printf("I am the parent process. My pid is %d\n", getpid());
value = 2;
}
printf("I am process %d and my value is %d\n", getpid(), value);

return 0;
}
Output :

5. Code :- Exec System Call:

#include <stdio.h>
#include <unistd.h>
int main()
{
printf("Executing ls command using execvp() system call.\n");
char *args[] = {"ls", "-l", NULL};
execvp(args[0], args);
printf("execvp() system call failed.\n");
return 0;
}

Output :

6. CODE :- Wait System Call:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int status;
pid = fork();
if (pid == 0) { // child process
printf("Child process is running.\n");
sleep(2);
printf("Child process is exiting.\n");
return 10;
} else if (pid > 0) { // parent process
printf("Parent process is waiting for child to complete.\n");
wait(&status); // wait for child to complete and get its exit status
printf("Child exit status: %d\n", WEXITSTATUS(status));
} else { // fork failed
printf("Fork failed.\n");
return 1;
}
return 0;
}

Output :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy