UNIX system programming & Compiler Design, Study Guides, Projects, Research of Computer Fundamentals

unix lab manual for 6th sem(vtu)

Typology: Study Guides, Projects, Research

2014/2015

Uploaded on 06/21/2015

kumar_v_sharma
kumar_v_sharma 🇮🇳

1 document

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sri Adichunchanagiri Shikshana Trust ®
SJB INSTITUTE OF TECHNOLOGY
BGS Health & Education City, Kengeri, Bangalore-60.
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
A
MANUAL FOR
VI SEMESTER
UNIX SYSTEM PROGRAMMING
&
COMPILER DESIGN LAB MANUAL
Subject Code: 10CSL68
Prepared By:
Darshan K.R
Lecturer, Dept.of CS&E
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download UNIX system programming & Compiler Design and more Study Guides, Projects, Research Computer Fundamentals in PDF only on Docsity!

Sri Adichunchanagiri Shikshana Trust ®

SJB INSTITUTE OF TECHNOLOGY

BGS Health & Education City , Kengeri, Bangalore-60.

DEPARTMENT OF

COMPUTER SCIENCE AND ENGINEERING

A

MANUAL FOR

VI SEMESTER

UNIX SYSTEM PROGRAMMING

COMPILER DESIGN LAB MANUAL

Subject Code: 10CSL

Prepared By:

Darshan K.R

Lecturer, Dept.of CS&E

Syllabus

List of Experiments for USP: Design, develop, and execute the following programs

  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
  2. Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using feature test macros.
  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.
  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.
  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
  6. Write a C/C++ program to illustrate the race condition.
  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.
  8. Write a C/C++ program to avoid zombie process by forking twice.
  9. Write a C/C++ program to implement the system function.
  10. Write a C/C++ program to set up a real-time clock interval timer using the alarm API.

List of Experiments for Compiler Design:

  1. Write a C program to implement the syntax-directed definition of “if E then S1” and “if E then S1 else S2”. (Refer Fig. 8.23 in the text book prescribed for 06CS62 Compiler Design, Alfred V Aho, Ravi Sethi, and Jeffrey D Ullman: Compilers- Principles, Techniques and Tools, Addison-Wesley, 2007).
  2. Write a yacc program that accepts a regular expression as input and produce its parse tree as output.

Note: In the examination each student picks one question from the lot of all 12 questions.

To Run the Program

[root@localhost /]# g++ limit.cpp [root@localhost /]# ./a.out

OUTPUT

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:

2. Write a C/C++ POSIX compliant program that prints the POSIX defined configuration

options supported on any given system using feature test macros.

To Open the Editor with filename

[root@localhost /]# gedit testmacro.cpp

#define _POSIX_SOURCE

#define _POSIX_C_SOURCE 199309L

#include

#include<unistd.h>

int main()

using namespace std;

#ifdef _POSIX_JOB_CONTROL

cout<<"System Supports Job Control feature"<<endl;

#else

cout<<"System doesnot support job control\n";

#endif

#ifdef _POSIX_SAVED_IDS

cout<<"System Supports saved set-UID and saved set-GID"<<endl;

#else

cout<<"System doesnot support saved set-UID\n";

#endif

#ifdef _POSIX_CHOWN_RESTRICTED

cout<<"System Supports Change Ownership feature:"<<endl;

#else

cout<<"System doesnot support change Ownership feature\n";

#endif

#ifdef _POSIX_NO_TRUNC

cout<<"System Supports Path truncation option:"<<endl;

#else

cout<<"System doesnot support Path truncation \n";

#endif

#ifdef _POSIX_VDISABLE

cout<<"System Supports Disable Character for files:"<<endl;

#else

cout<<"System doesnot support Disable Characters \n";

#endif

return 0;

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.

[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; }

To Run Program

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

OUTPUT

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

To Run the Program

[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

5a) Write a C/C++ program that outputs the contents of its Environment list

[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); }

To Run the Program

[root@localhost /]# cc env.c [root@localhost /]# ./a.out

OUTPUT

List of Environmental Variable

SSH_AGENT_PID=

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

6. Write a C/C++ program to illustrate the race condition.

[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); }

To Run the Program

[root@localhost /]# cc race.c [root@localhost /]# ./a.out

OUTPUT

output from child output from parent [root@localhost /]# ./a.out output from cohuitlpdu t from parent [root@localhost /]# ./a.out oouuttppuutt ffrroomm pcahrieldnt

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.

[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; }

To Run the Program

[root@localhost /]# cc zombie.c [root@localhost /]# ./a.out

OUTPUT

PID PPID STAT CMD

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] //Zombie Process 3168 3166 R+ ps -e -o pid,ppid,stat,cmd

9. Write a C/C++ program to implement the system function.

[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); }

To Run The Program

[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

E then S1 else S2”.

[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); } }

To Run the Program

[root@localhost /]# cc sdd.c [root@localhost /]# ./a.out