USP Manual - NoRestriction
USP Manual - NoRestriction
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
A
MANUAL FOR
VI SEMESTER
UNIX SYSTEM PROGRAMMING
&
COMPILER DESIGN
LAB MANUAL
Subject Code: 10CSL68
By:
Darshan K.R
Lecturer, Dept.of CS&E
Unix Systems Programming and Compiler Design Manual
Syllabus
List of Experiments for USP: Design, develop, and execute the following programs
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
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.
10. Write a C/C++ program to set up a real-time clock interval timer using the alarm API.
11. 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).
12. 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.
#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 199309L
#include<iostream.h>
#include<unistd.h>
int main()
{
int res;
if((res=sysconf(_SC_CLK_TCK))==-1)
cout<<"System doesnot support\n";
else
cout<<"Number of Clock Tick:"<<res<<endl;
if((res=sysconf(_SC_CHILD_MAX))==-1)
cout<<"System doesnot support\n";
else
cout<<"Maximum Number of Child Process that process can
create:"<<res<<endl;
if((res=pathconf(“/”,_PC_PATH_MAX))==-1)
cout<<"System doesnot support\n";
else
cout<<"Maximum Path Length:"<<res<<endl;
if((res=pathconf(“/”,_PC_NAME_MAX))==-1)
cout<<"System doesnot support\n";
else
cout<<"Maximum No.of Character in a
filename:"<<res<<endl;
if((res=sysconf(_SC_OPEN_MAX))==-1)
cout<<"System doesnot support\n";
else
cout<<"Maximum Number of opened files per
process:"<<res<<endl;
return 0;
}
Prepared By: Darshan.K.R, Dept.of CS&E, SJBIT Page 3
Unix Systems Programming and Compiler Design Manual
OUTPUT
2. Write a C/C++ POSIX compliant program that prints the POSIX defined configuration
options supported on any given system using feature test macros.
#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 199309L
#include<iostream.h>
#include<unistd.h>
int main()
{
#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;
OUTPUT
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 <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
struct flock fl = {F_WRLCK, SEEK_SET,0, 100,0 };
int fd;
char buf[50];
fl.l_pid = getpid();
if ((fd = open(argv[1], O_RDWR)) == -1)
{
perror("Can't open file");
exit(1);
}
if (fcntl(fd, F_SETLK, &fl) == -1)
{
perror("Can't set Exculsive Lock");
exit(1);
}
else if(fl.l_type!=F_UNLCK)
{
printf("File Region has been Exclusively Locked by
process:%d\n",fl.l_pid);
}
else
{
printf("File is not Locked\n");
}
To Run Program
Create a file, here we are creating a file with name demo with the
following Content:
OUTPUT
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
char * myfifo = "/tmp/myfifo"; /* create the FIFO (named pipe) */
mkfifo(myfifo, 0666);
fd = open(myfifo, O_WRONLY);
write(fd,"Hi", sizeof("Hi")); /* write "Hi" to the FIFO */
close(fd);
return 0;
}
Prepared By: Darshan.K.R, Dept.of CS&E, SJBIT Page 9
Unix Systems Programming and Compiler Design Manual
OUTPUT
Received: Hi
5a) Write a C/C++ program that outputs the contents of its Environment list
#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);
}
OUTPUT
List of Environmental Variable
--------------------------------
SSH_AGENT_PID=3008
HOSTNAME=localhost.localdomain
DESKTOP_STARTUP_ID=
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
KDE_NO_IPV6=1
GTK_RC_FILES=/etc/gtk/gtkrc:/root/.gtkrc-1.2-gnome2
WINDOWID=50331729
QTDIR=/usr/lib/qt-3.3
QTINC=/usr/lib/qt-3.3/include
USER=root
HOME=/root
SHLVL=2
GNOME_DESKTOP_SESSION_ID=Default
LOGNAME=root
QTLIB=/usr/lib/qt-3.3/lib
CVS_RSH=ssh
#include<iostream.h>
#include<unistd.h>
int main(int argc,char* argv[])
{
if(argc!=3)
{
cout<<"Usage ./a.out sourcefile destination file\n";
return 0;
}
if(link(argv[1],argv[2])==-1)
{
cout<<"Can't Link\n";
return 1;
}
else
{
cout<<"Files have been Linked\n";
}
return 0;
}
OUTPUT
#include<stdio.h>
#include<stdlib.h>
#include<error.h>
int main(void)
{
pid_t pid;
OUTPUT
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.
#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;
}
OUTPUT
#include<stdio.h>
#include <sys/wait.h>
#include<errno.h>
#include<stdlib.h>
int main()
{
pid_t pid;
OUTPUT
#include<sys/wait.h>
#include<errno.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
if (cmdstring == NULL)
return(1);
return(status);
}
int main()
{
int status;
exit(0);
}
OUTPUT
Sun Dec 30 08:38:10 IST 2012
root pts/0 2012-12-30 08:34 (:0.0)
10. Write a C/C++ program to set up a real-time clock interval timer using the alarm API.
[root@localhost /]# gedit alarm.c
#include<signal.h>
#include<stdio.h>
#include<unistd.h>
#include<errno.h>
void wakeup()
{
printf("Hello\n");
printf("---------------------------------\n");
}
int main()
{
int i;
struct sigaction action;
action.sa_handler=wakeup;
action.sa_flags=SA_RESTART;
sigemptyset(&action.sa_mask);
if(sigaction(SIGALRM,&action,0)==-1)
{
perror("sigaction");
}
while(1)
{
alarm(5);
pause();
printf("Waiting For Alarm\n");
}
return 0;
}
OUTPUT
Hello
---------------------------------
Waiting For Alarm
Hello
--------------------------------- (After 5 CPU Clockcycle)
Waiting For Alarm
Hello
--------------------------------- (After 5 CPU Clockcycle)
Waiting For Alarm
11. Write a C program to implement the syntax-directed definition of “if E then S1” and
“if E then S1 else S2”.
#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];
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;
}
Prepared By: Darshan.K.R, Dept.of CS&E, SJBIT Page 19
Unix Systems Programming and Compiler Design Manual
OUTPUT
format of if statement
example............
if(a<b)then(s,a);
if(a<b)then(s,a) else (s,b);
format of if statement
example............
if(a<b)then(s,a);
if(a<b)then(s,a) else (s,b);
12. Write a yacc program that accepts a regular expression as input and produce its parse
tree as output
%{
#include<ctype.h>
char str[20];
int i=0;
%}
%token id
%left '+''/''*''-'
%%
E:S {infix_postfix(str);}
S:S'+'T|S'-'T
|T
T:T'*'F|T'/'F
|F
F:id|'('S')'
;
%%
#include<stdio.h>
main()
{
printf("\n Enter the grammar:");
yyparse();
}
yyerror()
{
printf("invalid");
}
yylex()
{
char ch=' ';
while(ch!='\n')
{
ch=getchar();
str[i++]=ch;
if(isalpha(ch)) return id;
if(ch=='+'||ch=='*'||ch=='-'||ch=='/') return ch;}
str[--i]='\0';
return(0);
exit(0);
}
OUTPUT
VIVA Question
26. Explain different exec functions? Describe how their functioning differs from each
other.
27. Write short notes on Race condition
28. Write a program that execs an interpreter file.
29. What is process Identifier? Mention the commands for getting different ID’s of calling
process