usp lab manual

40
Page1 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING TABLE OF CONTENTS S. NO PROGRAMS PAGE NO A.I. 1. List of Experiments for USP: Write a C/C++ POSIX compliant program to check the following limits: (i) No. of clock ticks (ii) Max. no. of child processes (iii) Max. path length (iv) Max. no. of characters in a file name (v) Max. no. of open files/ process 7-15 2. Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using feature test macros. 16-23 3. Consider the last 100 bytes as a region. Write a C/C++ program to check whether the region is locked or not. If the region is locked, print pid of the process which has locked. If the region is not locked, lock the region with an exclusive lock, read the last 50 bytes and unlock the region. 24-30 4. Write a C/C++ program which demonstrates interprocess communication between a reader process and a writer process. Use mkfifo, open, read, write and close APIs in your program. 31-38 5. a) Write a C/C++ program that outputs the contents of its Environment list. b) Write a C / C++ program to emulate the unix ln command. 39-46 6. Write a C/C++ program to illustrate the race condition. 47-57 7. Write a C/C++ program that creates a zombie and then calls system to execute the ps command to verify that the process is zombie. 58-65

Upload: reshma-bj

Post on 24-Nov-2015

478 views

Category:

Documents


6 download

DESCRIPTION

VTU 6TH SEM USP LAB MANUAL WITH OUTPUT FOR EACH PROGRAMS :)

TRANSCRIPT

Page5

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERINGTable of Contents

Program 1:Aim: Write a C/C++ POSIX compliant program to check the following limits: (i) No. of clock ticks (ii) Max. no. of child processes(iii) Max. path length(iv) Max. no. of characters in a file name (v) Max. no. of open files/ processDescription: Program illustrates the use of sysconf, pathconf to display the run time limits.These limits are defined in unistd.h. It is used query general system wide, file related configuration limits.

Program:#define _POSIX_SOURCE#define _POSIX_C_SOURCE 199309L#include#includeint main(){ int res; if((res=sysconf(_SC_CLK_TCK))==-1) printf("error"); else printf("no. of clock ticks %d \n",res);

if((res=sysconf(_SC_CHILD_MAX))==-1) printf("error"); else printf("no. of child processes %d\n",res);

if((res=pathconf("/",_PC_PATH_MAX))==-1) printf("error"); else printf("Max. no. of characters in a pathname:%d\n",res);

if((res=pathconf("/",_PC_NAME_MAX))==-1) printf("error"); else printf(" Max. no. of characters in a filename:%d\n",res);

if((res=sysconf(_SC_OPEN_MAX))==-1) printf("error"); else printf("Max. no. of open files:%d\n",res); return 0;}Result :

Program 2Aim: Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using feature test macros.Description: Program prints the POSIX defined configuration options supported on any given system. UNIX features are implemented on a POSIX conforming system. Thus, POSIX.1 defines a set of feature test macros, which, if defined on a system, System has implemented the corresponding features. These test macros, if defined, found in header.Program:#define _POSIX_SOURCE#define _POSIX_C_SOURCE 199309L#include#includeint main(){#ifdef _POSIX_JOB_CONTROL printf("system supports job control\n");#else printf("system does not support job control\n");#endif#ifdef _POSIX_SAVED_IDS printf("system supports saved ids\n");#else printf("system does not supports saved ids \n");#endif#ifdef _POSIX_CHOWN_RESTRICTED printf("system supports chown restricted\n");#else printf("system does not support chown restricted \n");#endif#ifdef _POSIX_NO_TRUNC printf("system supports no_trunc\n");#else printf("system does not support no_trunc \n");#endif#ifdef _POSIX_VDISABLE printf("system supports vdisable \n");#else printf("system does not support vdisable \n");#endifreturn 0;}

Result:

Program 3Aim: Consider the last 100 bytes as a region. Write a C/C++ program to check whether the region is locked or not. If the region is locked, print pid of the process which has locked. If the region is not locked, lock the region with an exclusive lock, read the last 50 bytes and unlock the region.Description: Program illustrates the use of fcntl for file locking. We can set the shared lock/exclusive lock to the specified region. File locking is applicable for regular files. It allows us to impose a lock on a file so that other processes cannot modify the file until it is unlocked by the process. Program:#include#include#include#include#include#includeusing namespace std;int main(int argc,char* argv[]){struct flock fvar;char buffer[100];int fdesc;while(--argc>0){if((fdesc=open(*++argv,O_RDWR))==-1){printf("error\n");continue;}fvar.l_type=F_WRLCK;fvar.l_whence=SEEK_END;fvar.l_start=-100;fvar.l_len=100;int flag=1;

printf("\nENTER any key to getlock\n");while(getchar()=='y');

/* Attempt to set an exclusive lock on the entire file*/while(fcntl(fdesc,F_SETLK,&fvar)==-1){if(fcntl(fdesc,F_GETLK,&fvar)!=-1 &&fvar.l_type!=F_UNLCK&& flag==1 && fvar.l_pid ){ cout