Monday, December 26, 2011

Exercise Program 1

/* 1. Program to print the process id */

Algorithm:

1. Include necessary header files for using systems calls.

2. Make necessary declaration.

3. Get the process identification number and parent process identification number using getpid() and getppid() system calls

4. Display the process id’s

#include

#include

#include

main()

{

int pid,ppid;

pid=getpid();

ppid=getppid();

printf("\n Process Id is %d\n",pid);

printf("\n Parent Process Id is %d\n",ppid);

}

OUTPUT

process ID is 5198

parent process ID is 5129

/* 2. Program using fork system call */

#include

#include

#include

main()

{

printf("\n This is to demonstrate the fork()");

fork();

printf("\nAfter fork()");

}

OUTPUT

this is to demonstrate fork()

after fork()

/* 3. fork and pid */

#include

#include

#include

main()

{

int pid;

pid=fork();

if(pid==0)

{

printf("\n I am the child, my process ID is %d ",getpid());

printf("\n I am the child's parent process ID is %d ",getppid());

}

else

{

printf("\n I am the parent, my process ID is %d ",getpid());

printf("\n I am the parent's parent process ID is %d ",getppid());

}

}

OUTPUT

i am the parent, my process ID is 5273

i am the parent's parent,process ID is 5129

I am the child, my process ID is 5274

I am the child's parent, process ID is 1

/* 4. orphan process */

#include

#include

#include

main()

{

int pid;

pid=fork();

if(pid==0)

{

printf("\n I am the child, my process ID is %d ",getpid());

printf("\n I am the child's parent process ID is %d ",getppid());

sleep(10);

printf("\n I am the child, my process ID is %d ",getpid());

printf("\n I am the child's parent process ID is %d ",getppid());

}

else

{

printf("\n I am the parent, my process ID is %d ",getpid());

printf("\n I am the parent's parent process ID is %d ",getppid());

}

}

OUTPUT

I am the child, my process ID is 5301

I am the parent, my process id is 5300

I am the parent's parent, my process ID is 5129

[it2-21@localhost ~]$ I am the

child's parent, my process ID is 5300

I am the child, my process Id is 5301

I am the child's parent, my process ID is 1

/* 5. exec system call */

#include

#include

#include

main()

{

execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);

}

OUTPUT
total 384

-rw-rw-r-- 1 it2-21 it2-21 359 Oct 14 08:24 pol.cpp

-rw-rw-r-- 1 it2-21 it2-21 200 Dec 8 11:35 forks.c

-rw-rw-r-- 1 it2-21 it2-21 102 Dec 14 10:48 exec1.c

-rw-rw-r-- 1 it2-21 it2-21 283 Dec 14 11:06 wait.c

-rw-rw-r-- 1 it2-21 it2-21 246 Dec 14 11:22 exit.c

-rw-rw-r-- 1 it2-21 it2-21 214 Dec 14 11:25 exit1.c

-rw-rw-r-- 1 it2-21 it2-21 393 Dec 14 12:12 opre.c

-rw-rw-r-- 1 it2-21 it2-21 367 Dec 14 12:13 fileinfo.c

-rw-rw-r-- 1 it2-21 it2-21 0 Dec 14 12:20 read.cpp

-rw-rw-r-- 1 it2-21 it2-21 1786 Dec 18 09:50 poly.cpp

-rw-rw-r-- 1 it2-21 it2-21 153 Dec 21 11:31 op.c

-rw-rw-r-- 1 it2-21 it2-21 0 Dec 21 11:32 buf,c

-rw-rw-r-- 1 it2-21 it2-21 197 Dec 21 11:32 buf.c

---------x 1 it2-21 it2-21 0 Dec 21 11:33 fn

/* 6. exec system call */

#include

#include

#include

main()

{

execl("/bin/date", "date", NULL);

}

OUTPUT

Mon Jan 11 11:08:50 UTC 2010

/* 7. wait system call */

/* process synchronization */

#include

#include

#include

main()

{

int pid,i=0;

printf("\n Ready to fork");

pid=fork();

if(pid==0)

{

printf("\n Child starts ");

for(i=0;i<1000;i++);

printf("\n Child ends ");

}

else

{

wait(0);

for(i=0;i<1000;i++);

printf("\n Parent process ends ");

}

}

OUTPUT

ready to fork

child starts

child ends

ready to fork

parent process ends

/* 8. Program using exit system call */

#include

#include

#include

main()

{

int p;

p=fork();

if(p==0)

{

printf("\n Child created ");

exit(0);

printf("\n Process ended ");

}

if(p<0)

{

printf("\n Cannnot create child ");

exit(-1);

printf("\n Process ended ");

}

}

OUTPUT

child created

/* 9. Program using exit and wait system call */

#include

#include

#include

main()

{

unsigned int status;

if ( fork () == 0 ) { /* == 0 means in child */

scanf ("%d", &status);

exit (status);

}

else { /* != 0 means in parent */

wait (&status);

printf("child exit status = %d\n", status > 8);

}

}

OUTPUT

child exit status=1

/* 10. Program using lstat() system call */

#include

#include

main(int argc,char **argv)

{

struct stat statbuf;

if(lstat(argv[1],&statbuf)==-1)

printf("Error : cannot find file information ");

else

{

printf("\n File %s ", argv[1]);

printf("\n Inode number %d",statbuf.st_ino);

printf("\n UID %d",statbuf.st_uid);

printf("\n GID %d",statbuf.st_gid);

printf("\n File size in bytes %d",statbuf.st_size);

}

}

OUTPUT

./a.out opre.c

file opre.cI node number 1387260UID 998GID 999file size in bytes 393

/* 11. Program using opendir and readdir system call */

#include

#include

#include

int main(int argc, char *argv[])

{

DIR *dp;

struct dirent *dirp;

if (argc != 2) {

printf("a single argument (the directory name) is required\n");

exit(1);

}

if ( (dp = opendir(argv[1])) == NULL) {

printf("can't open %s\n",argv[1]);

exit(1);

}

while ( (dirp = readdir(dp)) != NULL)

printf("%s %d\n",dirp->d_name,dirp->d_ino);

closedir(dp);

exit(0);

}

OUTPUT

./a.out

grep.c 1387273

simu.c 1387274

a.out 1387265

. 1387247

hi 1387233

.. 1387211

rem.c 1387276

No comments: