USP Lab Manual
USP Lab Manual
USP Lab Manual
Lab Manual
Program 1: 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
#define _POSIX_SOURCE #include<iostream> #include<stdio.h> #include<unistd.h> #include<limits.h> using namespace std; int main() { long ret; if((ret=pathconf("/",_PC_PATH_MAX))==-1) { perror("pathconf"); return -1; } else cout<<"Maximum Path Length = "<<ret<<endl; if((ret=pathconf("/",_PC_NAME_MAX))==-1) { perror("pathconf"); return -1; } else cout<<"Maximum Number of Characters in a File name = "<<ret<<endl; if((ret=sysconf(_POSIX_CHILD_MAX))==-1) { perror("sysconf"); return -1; } else cout<<"Maximum Number of Child Processes = "<<ret<<endl; Dept of CSE, PESIT Bangalore South Campus. Page 1
Unix System Programming And Compiler Design Laboratory ( 10CSL68 ) if((ret=sysconf(_SC_OPEN_MAX))==-1) { perror("sysconf"); return -1; } else cout<<"Maximum Number of Open Files/Processes = "<<ret<<endl; if((ret=sysconf(_SC_CLK_TCK))==-1) { perror("sysconf"); return -1; } else cout<<"Number of Clock Ticks per Second = "<<ret<<endl; return 0; }
Program 2 :Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using feature test macros.
#include <iostream> #include <unistd.h> #include <stdlib.h> using namespace std; int main() { #ifdef _POSIX_JOB_CONTROL cout<<"System supports job control\n"; #else cout<<"System does not support job control\n"; #endif #ifdef _POSIX_SAVED_IDS cout<<"System supports saved set-UID and saved set-GID\n"; #else cout<<"System does not support saved set-UID and saved set-GID\n"; #endif #ifdef _POSIX_CHOWN_RESTRICTED
Page 2
Unix System Programming And Compiler Design Laboratory ( 10CSL68 ) cout<<"chown restricted option is: "<<_POSIX_CHOWN_RESTRICTED<<endl; #else cout<<"System does not support system-wide chown_restricted option\n"; #endif #ifdef _POSIX_NO_TRUNC cout<<"Pathname truncation option is: "<< _POSIX_NO_TRUNC<<endl; #else cout<<"System does not support system-wide pathname truncation option\n"; #endif #ifdef _POSIX_VDISABLE cout<<"Disable character for terminal files is: "<<_POSIX_VDISABLE<<endl; #else cout<<"System does not support _POSIX_VDISABLE\n"; #endif return 0; }
Program 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.
#include <iostream> #include <iomanip> #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<fcntl.h> using namespace std; int main(int argc, char** argv) { int fd; struct flock fLock; char buf[80]; if((fd=open(argv[1],O_RDWR))==-1) { perror("open"); Dept of CSE, PESIT Bangalore South Campus. Page 3
Unix System Programming And Compiler Design Laboratory ( 10CSL68 ) return -1; } //Set lock parameters fLock.l_type=F_WRLCK; fLock.l_whence=SEEK_END; fLock.l_start=-100; fLock.l_len=100;
/* Attempt to set an exclusive lock on a File */ while(fcntl(fd,F_SETLK,&fLock)==-1) { /* Set Lock Fails, find out who had locked the file */ while((fcntl(fd,F_GETLK,&fLock)!=-1)) { cout << endl << argv[1] << " is Locked by the Process " << fLock.l_pid << " from " << fLock.l_start << " for " << fLock.l_len << " bytes for " << (fLock.l_type==F_WRLCK?"write":"read") << endl; return 1; } } cout << "\nLock aquired on the File " << argv[1] << " by process with ID " << getpid() << endl; sleep(30); lseek(fd,-50,SEEK_END); if(read(fd,buf,50)==-1) { perror("read"); } else { cout << "\nReading 50 bytes from file\n"; cout << buf; cout << endl; } fLock.l_type = F_UNLCK; fLock.l_whence = SEEK_SET; fLock.l_start = 0; fLock.l_len = 0; if(fcntl(fd,F_SETLKW,&fLock)==-1) perror("fcntl"); else cout << endl << argv[1] << " unlocked by process " << getpid() << endl; return 0; }
Page 4
Program 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.
/*File : reader_ipcomm.c */
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #define FIFO_NAME "MY_FIFO" int main(void) { char s[300]; int num, fd; // mknod(FIFO_NAME, S_IFIFO|0666, 0); mkfifo(FIFO_NAME, S_IFIFO|S_IRWXU|S_IRWXG|S_IRWXO); printf("Waiting for Writers... \n"); fd=open(FIFO_NAME, O_RDONLY); printf("Got a Writer...\n"); do { if((num=read(fd,s,300))==-1) { perror("read"); } else { s[num]='\0'; printf("Receiver:Read %d bytes: \"%s\" \n",num,s); } }while(num>0); return 0; }
/*File
: writer_ipcomm.c */
Unix System Programming And Compiler Design Laboratory ( 10CSL68 ) #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #define FIFO_NAME "MY_FIFO" int main(void) { char s[300]; int num, fd; // mknod(FIFO_NAME, S_IFIFO|0666, 0); mkfifo(FIFO_NAME, S_IFIFO|S_IRWXU|S_IRWXG|S_IRWXO); printf("Waiting for Readers... \n"); fd=open(FIFO_NAME, O_WRONLY); printf("Got a Reader... type Something: \n"); while(gets(s), !feof(stdin)) { if((num=write(fd,s,strlen(s)))==-1) { perror("write"); } else { printf("Writer: wrote %d bytes \n",num); } } return 0; }
Unix System Programming And Compiler Design Laboratory ( 10CSL68 ) { strcpy(f1,argv[1]); strcpy(f2,argv[2]); } rcode=link(f1,f2); if(rcode==0) { printf("Link Successfully created\n"); }
else { printf("Link creation failed: %d\n", errno); printf("error number is : %d \n", errno); strcpy(err, strerror(errno)); perror("ln"); puts(err); } } else { strcpy(f1,argv[2]); strcpy(f2,argv[3]); rcode=symlink(f1,f2); if(rcode==0) { printf("\n Soft link created successsfully \n"); } else { printf("Link creation failed: %d\n", errno); strcpy(err, strerror(errno)); puts(err); } } return 0; }
Program 5b :#include <stdio.h> #include <stdlib.h> int main(char* argv[], int argc, char* envp[]) { int index=0; while(envp[index]) { printf("%s \n",envp[index++]);
Page 7
static void charatatime(char *); int main() { pid_t pid, pid2; if((pid=fork())<0) { perror("fork"); return -1; } else if (pid==0) { charatatime("\n output from child \n"); } else { charatatime("\n output from parent \n"); } exit(0); } static void charatatime(char * str) { char *ptr; int c; setbuf(stdout, NULL); for(ptr=str; c=*ptr++;) { putc(c,stdout); } }
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.
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/wait.h> int main() { pid_t pid,retpid; char cmd[50]; printf("Parent Process with process id %d starts...\n",getpid()); if((pid=fork())==-1) { perror("fork"); return -1; } else if(pid==0) { printf("Child Process Created\n"); printf("Child Pid:%d Parent Pid:%d\n",getpid(),getppid()); sleep(5); _exit(0); } else { sleep(20); printf("Parent Process Resumes Its Execution,,,,\n"); } return 0; }
int main() { pid_t pid1,pid2; printf("Parent process P starts... PID : %d \n", getpid());
Page 9
Page 10
Program 10 :Write a C/C++ program to set up a real-time clock interval timer using the alarm API.
#include #include #include #include #include <stdio.h> <stdlib.h> <signal.h> <unistd.h> <sys/time.h>
void alarm_handler (int signum) { printf("\n Timer Hit! \n"); } void func(int duration) { struct itimerval delay; int ret; signal(SIGALRM, alarm_handler); delay.it_value.tv_sec=duration; delay.it_value.tv_usec=0; delay.it_interval.tv_sec=0; delay.it_interval.tv_usec=0; ret=setitimer(ITIMER_REAL, &delay, NULL); if(ret) {
Page 11
Program 11 :Write a C program to implement the syntax-directed definition of if E then S1 and if E then S1 else S2.
#include <stdlib.h> #include <stdio.h> #include <string.h> char input[60], stmt[3][60]; int cur,len,i,j; void gen() { int L1=101,L2=102,L3=103; printf("if %s goto %d \n", stmt[0],L1); printf("\n goto %d \n", L2); printf("%d : %s \n", L1,stmt[1]); if(cur<3) { printf("%d : STOP \n",L2); } else { printf("goto %d \n",L3); printf("%d : %s \n", L2, stmt[2]); printf("%d : STOP \n",L3); } }
int main() { printf("format of if statement \n example: \n"); printf("if (a<b) then (s=a); \n"); printf("if (a<b) then (s=a) else (s=b); \n"); Dept of CSE, PESIT Bangalore South Campus. Page 12
Unix System Programming And Compiler Design Laboratory ( 10CSL68 ) printf("Enter the statement \n"); gets(input); len=strlen(input); int index=0; for(i=0;i<len && input[i]!=';';i++) { if(input[i]=='(') { index=0; for(j=i; input[j-1]!=')';j++) stmt[cur][index++]=input[j]; cur++; i=j; } } gen(); return 0; }
Program 12 :Write a yacc program that accepts a regular expression as input and produce its parse tree as output.
%{ #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<string.h> #define max 100 int getreindex(const char *); signed char productions[max][max]; int count=0,i,j; char temp[max+max],temp2[max+max]; %} %token ALPHABET %left '|' %left '.' %nonassoc '*''+' %% S :re'\n' { printf("this is the rightmost derivation--\n"); for(i=count-1;i>=0;--i) { if(i==count-1) { printf("\nre-->"); strcpy(temp,productions[i]); printf("%s",productions[i]); } else { printf("\n-->");
Page 13
re: ALPHABET { temp[0]=yylval;temp[1]='\0'; strcpy(productions[count++],temp); } |'('re')' {strcpy(productions[count++],"(re)");} |re'*' {strcpy(productions[count++],"re*");} |re'+' {strcpy(productions[count++],"re+");} |re'|'re {strcpy(productions[count++],"re|re");} |re'.'re {strcpy(productions[count++],"re.re");} ; %% int main(int argc,char **argv) { yyparse(); return 0; } yylex() { signed char ch=getchar(); yylval=ch; if(isalpha(ch)) return ALPHABET; return ch; } yyerror() { fprintf(stderr,"invalid regular expression!\n"); exit(1); } int getreindex(const char *str) { int i=strlen(str)-1; for(;i>=0;i--) if(str[i]=='e' && str[i-1]=='r') return i-1; }
Page 14