


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
unix lab manual for 6th sem(vtu)
Typology: Study Guides, Projects, Research
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















Sri Adichunchanagiri Shikshana Trust ®
BGS Health & Education City , Kengeri, Bangalore-60.
List of Experiments for USP: Design, develop, and execute the following programs
List of Experiments for Compiler Design:
Note: In the examination each student picks one question from the lot of all 12 questions.
[root@localhost /]# g++ limit.cpp [root@localhost /]# ./a.out
Number of Clock Tick: Maximum Number of Child Process that process can create: Maximum Path Length: Maximum No.of Character in a filename: Maximum Number of opened files per process:
[root@localhost /]# gedit testmacro.cpp
[root@localhost /]# gedit lock.c
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <fcntl.h> #include <unistd.h>
int main(int argc, char argv[]) { / l_type l_whence l_start l_len l_pid*/ struct flock fl = {F_UNLCK, SEEK_SET, 0, 100, 0 }; int fd; int fsize,offset; char buf[50]; if ((fd = open(argv[1], O_RDWR)) == -1) { perror("Can't open file"); exit(1); } printf("File is Not Locked by any Process\n"); printf("Press Enter to Lock the File\n"); printf("---------------------------------\n"); getchar(); fl.l_type = F_WRLCK; fl.l_pid = getpid(); if (fcntl(fd, F_SETLK, &fl) == -1) { perror("Can't set Exculsive Lock"); exit(1); }
else if(fl.l_type!=F_UNLCK) { printf("File has been Exculsively Locked by process:%d\n",fl.l_pid); } else { printf("File is not Locked\n"); }
printf("Press ENTER to Release lock:\n"); getchar(); fl.l_type = F_UNLCK; printf("File has been Unlocked\n"); fsize=lseek(fd,0,SEEK_END); offset=fsize-50;
lseek(fd,offset,SEEK_SET); read(fd,buf,50); printf("Last 50 Byte Content in the file is\n"); printf("====================================\n"); printf("%s\n",buf); return 0; }
Create a file, here we are creating a file with name demo with the following Content:
Consider the last 100 bytes as a region. Write a C/C++ program to check whether the region is locked.
[root@localhost /]# cc lock.c [root@localhost /]# ./a.out demo
File is Not Locked by any Process Press Enter to Lock the File
File has been Exclusively Locked by process: 4087 Press Any Key to release lock:
File has been Unlocked Last 50 Byte Content in the file is ==================================== /C++ program to check whether the region is locked
[root@localhost /]# cc writer.c [root@localhost /]# ./a.out
Run Reader process to read the FIFO File
After this Open New Terminal by pressing shift+ctrl+N or Go to File-
Open Terminal
[root@localhost /]# cc reader.c [root@localhost /]# ./a.out
OUTPUT
Received: Hi
[root@localhost /]# gedit env.c
#include<stdio.h> #include<stdlib.h>
int main() { int i; char **ptr; extern char **environ; printf("List of Environmental Variable\n"); printf("--------------------------------\n"); for (ptr = environ; *ptr != 0; ptr++) printf("%s\n", *ptr);
exit(0); }
[root@localhost /]# cc env.c [root@localhost /]# ./a.out
OUTPUT
HOSTNAME=localhost.localdomain DESKTOP_STARTUP_ID= SHELL=/bin/bash TERM=xterm HISTSIZE= KDE_NO_IPV6= GTK_RC_FILES=/etc/gtk/gtkrc:/root/.gtkrc-1.2-gnome WINDOWID= QTDIR=/usr/lib/qt-3. QTINC=/usr/lib/qt-3.3/include USER=root HOME=/root SHLVL= GNOME_DESKTOP_SESSION_ID=Default LOGNAME=root QTLIB=/usr/lib/qt-3.3/lib CVS_RSH=ssh
[root@localhost /]# gedit race.c
#include<stdio.h> #include<stdlib.h> #include<error.h>
static void charatatime(char *);
int main(void) { pid_t pid;
if ((pid = fork()) < 0) { printf("fork error"); } else if (pid == 0) { charatatime("output from child\n"); } else { charatatime("output from parent\n"); } exit(0); }
static void charatatime(char *str) { char *ptr; int c;
setbuf(stdout, NULL); /* set unbuffered */ for (ptr = str; (c = *ptr++) != 0; ) putc(c, stdout); }
[root@localhost /]# cc race.c [root@localhost /]# ./a.out
output from child output from parent [root@localhost /]# ./a.out output from cohuitlpdu t from parent [root@localhost /]# ./a.out oouuttppuutt ffrroomm pcahrieldnt
[root@localhost /]# gedit zombie.c
#include <stdlib.h> #include <sys/types.h> #include <unistd.h>
int main () { pid_t child_pid; /* Create a child process. / child_pid = fork (); if (child_pid == 0) { exit (0); / This is the child process.Exit immediately. / } else { sleep(3); / This is the parent process. Sleep for a minute. */ system("ps -e -o pid,ppid,stat,cmd"); } return 0; }
[root@localhost /]# cc zombie.c [root@localhost /]# ./a.out
1 0 Ss init [5] 2 1 S [migration/0] 3 1 SN [ksoftirqd/0] 4 1 S [watchdog/0] 5 1 S [migration/1] 6 1 SN [ksoftirqd/1] 7 1 S [watchdog/1] 8 1 S [migration/2] 9 1 SN [ksoftirqd/2] 10 1 S [watchdog/2] 3087 3084 S gnome-pty-helper 3088 3084 Ss bash 3166 3088 S+ ./a.out 3167 3166 Z+ [a.out]
[root@localhost /]# gedit system.c
#include<sys/wait.h> #include<errno.h> #include<unistd.h> #include<stdio.h> #include<stdlib.h>
int system1(const char *cmdstring) { pid_t pid; int status;
if (cmdstring == NULL) return(1);
if ((pid = fork()) < 0) { status = -1; } else if (pid == 0) { /* child */ execl("/bin/sh", "sh", "-c", cmdstring, (char )0); _exit(127); / execl error / } else / parent / while (waitpid(pid, &status, 0) < 0) { if (errno != EINTR) status = -1; / error other than EINTR from waitpid() */ break; }
return(status); }
int main() { int status;
if ((status = system1("date")) < 0) printf("system() error");
if ((status = system1("who")) < 0) printf("system() error");
exit(0); }
[root@localhost /]# cc system.c [root@localhost /]# ./a.out
OUTPUT
Sun Dec 30 08:38:10 IST 2012 root pts/0 2012-12-30 08:34 (:0.0)
11. Write a C program to implement the syntax-directed definition of “if E then S1” and “if
[root@localhost /]# gedit sdd.c
#include<stdio.h> #include<stdlib.h> #include<string.h> int parsecondition(char[],int ,char *,int); void gen(char[],char[],char[],int);
int main() { int counter=0,stlen=0,elseflag=0; char stmt[60]; char strB[54]; char strS1[50]; char strS2[45];
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\n"); printf("enter the statement\n"); scanf("%s",&stmt);
stlen=strlen(stmt); counter=counter+2; counter=parsecondition(stmt,counter,strB,stlen); if(stmt[counter]==')') counter++; counter=counter+3; counter=parsecondition(stmt,counter,strS1,stlen); if(stmt[counter+1]==';') { printf("\n parsing the input statement...\n"); gen(strB,strS1,strS2,elseflag); return 0; } if(stmt[counter]==')') counter++; counter=counter+3; counter=parsecondition(stmt,counter,strS2,stlen); counter=counter+2; if(counter==stlen) { elseflag=1; printf("\n parsing the input statement"); gen(strB,strS1,strS2,elseflag); return 0; } return 0; }
int parsecondition(char input[],int cntr , char *dest, int totallen) { int index=0,pos=0; while(input[cntr]!='(' && cntr<=totallen) cntr++; if(cntr>=totallen) return 0; index=cntr; while(input[cntr]!=')') cntr++; if(cntr>=totallen) return 0; while(index<=cntr) dest[pos++]=input[index++]; dest[pos]='\0'; return cntr; } void gen(char B[],char S1[],char S2[],int elsepart) { int Bt=101,Bf=102,Sn=103; printf("\n\t if %s goto %d",B,Bt); printf("\n\t goto %d",Bf); printf("\n %d:",Bt); printf("%s",S1); if(!elsepart) printf("\n %d:",Bf); else { printf("\n\t goto %d",Sn); printf("\n %d : %s",Bf,S2); printf("\n %d:",Sn); } }
[root@localhost /]# cc sdd.c [root@localhost /]# ./a.out