Professional Documents
Culture Documents
Assessment: 1
(Submission Date: 8/8/19) 18BIT0478
KAUSTUBH VIKAS BANTE
A)
Both processes start their execution right after the system call fork().
Since both processes have identical but separate address spaces, those variables initialized before
the fork() call have the same values in both address spaces.
Since every process has its own address space, any modifications will be independent of the others.
In other words, if the parent changes the value of its variable, the modification will only affect the
variable in the parent process's address space. Other address spaces created by fork() calls will not
be affected even though they have identical variable names.
#include<stdio.h>
main(int arc,char*ar[])
int pid;
char s[100];
pid=fork();
if(pid<0)
printf("error");
else if(pid>0)
wait(NULL);
execlp("cat","cat",ar[1],(char*)0);
else
printf("\nChild process:");
write(1,s,strlen(s));
printf(" ");
printf(" ");
printf(" ");
execvp(ar[2],&ar[2]);
B:
#include<sys/types.h>
#include<dirent.h>
#include<stdio.h>
main(int c,char* arg[]) {
DIR *d;
struct dirent *r;
int i=0;
d=opendir(arg[1]);
printf("\n\t NAME OF ITEM \n");
while((r=readdir(d)) != NULL) {
printf("\t %s \n",r->d_name);
i=i+1;
}
printf("\n TOTAL NUMBER OF ITEM IN THAT DIRECTORY IS %d
\n",i);
OUTPUT:
B)
#include<stdio.h>
#include<sys/stat.h>
#include<time.h>
main(int ag,char*arg[])
{ char buf[100];
struct stat s;
int fd1,fd2,n;
fd1=open(arg[1],0);
fd2=creat(arg[2],0777);
stat(arg[2],&s);
if(fd2==-1)
printf("ERROR IN CREATION");
while((n=read(fd1,buf,sizeof(buf)))>0)
if(write(fd2,buf,n)!=n)
{ close(fd1);
close(fd2);
} printf("\t\n UID FOR FILE.......>%d \n FILE ACCESS TIME.....>%s \n FILE MODIFIED TIME........>%s \n
FILE I-NODE NUMBER......>%d \n PERMISSION FOR FILE.....>%o\n\n",s.st_uid,ctime
(&s.st_atime),ctime(&s.st_mtime),s.st_mode);
close(fd1);
close(fd2);
C)
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t p;
printf(“I am the original process with pId %d and ppid
%d\n”,getpid(),getppid())
/* create child process */
p=fork();
if(p==0) {
/* fork() returns Zero to child */
sleep(10);
}
printf("The child process pid is %d parent pid %d\n", getpid(), getppid());
/*parent/child waits for 20 secs and exits*/
sleep(20);
printf("\nProcess %d is done its Parent pid %d...\n", getpid(), getppid());
return 0;
}
OUTPUT:
D)
#include<stdio.h>
#include<pthread.h>
#include<tgmath.h>
void *factorial(void *p);
int fact(int n);
int main(){
pthread_t tid1;
pthread_attr_t attr; // set of thread attributes
}
int fact(int n){
if(n==0 || n==1)
return 1;
else
return n*fact(n-1);
}
void *factorial(void *p){
int i,num1;
printf("Thread 1 (factorial) : ");
printf("Enter Number: ");
scanf("%d",&num1);
printf("Factorial is: %d\n",fact(num1));
pthread_exit(0);
}
OUTPUT:
E)
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
char c,num[10];
int n,len,i;
while(1)
{
printf("\nWhat is your number");
gets(num);//Read input from console
if(strcmp(num,"q")==0)// Quit if input is q
exit(0);
n=atoi(num);// String to number conversion
if(n>2)// Wrong input checking
{
collatz(n);// Function calling
}
else
{
printf("enter valid input...\n");
}
}
return 0;
int status = 0;
while(i != 1){
printf("%d\t", i);
i /= 2;
else{// if n is odd
i = 3*i + 1;
printf("1\n");
exit(1);
}
while ((wpid = wait(&status)) > 0)
return;
OUTPUT:
F)
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSIZE 27
int main() {
int shmid;
int *shm;
int *n;
if(fork() == 0) {
shmid = shmget(2009, SHMSIZE, 0);
shm = shmat(shmid, 0, 0);
n = shm;
int i;
for(i=0; i<5; i++) {
printf("Enter character<%d>: ", i);
scanf("%d", n++);
}
printf ("Child wrote <%d>\n",shm);
shmdt(shm);
}
else {
wait();
int *s;
shmid = shmget(2009, SHMSIZE, 0666 | IPC_CREAT);
shm = shmat(shmid, 0, 0);
s = shm;
wait(NULL);
printf ("Parent reads <%d>\n",shm) ;
shmdt(shm);
shmctl(shmid, IPC_RMID, NULL);
}
return 0;
}
OUTPUT: