You are on page 1of 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