0% found this document useful (0 votes)
31 views9 pages

CSE2005 LAB Feb 25

The document contains code snippets from multiple C programs that demonstrate using fork() to create child processes. The snippets show printing output from the parent and child processes, incrementing/decrementing variables after forking, and using fork() multiple times to create multiple child processes. One snippet also demonstrates creating a thread using pthread_create() and printing from the thread.

Uploaded by

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

CSE2005 LAB Feb 25

The document contains code snippets from multiple C programs that demonstrate using fork() to create child processes. The snippets show printing output from the parent and child processes, incrementing/decrementing variables after forking, and using fork() multiple times to create multiple child processes. One snippet also demonstrates creating a thread using pthread_create() and printing from the thread.

Uploaded by

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

CSE2005 LAB feb 25

Abhishek Kandel(19BCE2629)
#include<stdio.h>

#include<unistd.h>//contains fork prototype

int main(void)

printf("hello world\n");

fork( );

fork( );

printf("i am after forking\n");

printf("\t I am process %d.\n",getpid());

Output:
#include<stdio.h>

#include<unistd.h>//contains fork prototype

int main(void)

int pid;

printf("hello world\n");

printf("i am he parent process and the pid is %d.\n",getpid());

printf("here i am before use of forking\n");

pid=fork();

printf("here i am just after forking\n");

if (pid==0){

printf("i am the child process and pid is %d.\n",getpid());

else{

printf("i am the parent process and the pid is %d.\n",getpid());

Output:
#include <stdio.h>

#include <sys/types.h>

int main()

fork();

fork();

fork();

printf("hello\n");

return 0;

Output:

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

void forkexample()

// child process because return value zero if (fork() == 0)

printf("Hello from Child!\n");

// parent process because return value non-zero. else


printf("Hello from Parent!\n");

int main()

forkexample();

return 0;

Output:

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

void forkexample()

int x = 1;

if (fork() == 0)
printf("Child has x = %d\n", ++x);

else

printf("Parent has x = %d\n", --x);

int main()

forkexample();

return 0;

Output:

#include<stdio.h>

#include<unistd.h>

main(void){

printf("before forking\n");

fork();

printf("after first forking\n");

fork();

printf("after second forking\n");


printf("\t\thello world form process %d\n",getpid());

Output:

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h> //Header file for sleep(). man 3 sleep for details.

#include <pthread.h>

// A normal C function that is executed as a thread


// when its name is specified in pthread_create()

void *myThreadFun(void *vargp)

sleep(1);

printf("Printing from Thread \n");

return NULL;

int main()

pthread_t thread_id;

printf("Before Thread\n");

pthread_create(&thread_id, NULL, myThreadFun, NULL);

pthread_join(thread_id, NULL);

printf("After Thread\n");

exit(0);

Output:
Write a C program to kill a process by specifying its name rather than its PID.

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <error.h>

#include <signal.h>

#include <unistd.h>

#include <syslog.h>

int main()

FILE *getPIDS;

char line[130];

pid_t killpid;

// setuid to that of root//

pid_t mypid = getpid();

pid_t myppid = getppid();

getPIDS = popen("pidof -x yes","r");

while (fgets(line,sizeof line,getPIDS)) {

printf("KILL PID: %s",line);

kill(line,SIGKILL);

}
Output:

THANK YOU!!!

You might also like