Professional Documents
Culture Documents
USP Manual
USP Manual
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
Syllabus
List of Experiments for USP: Design, develop, and execute the following programs
1.
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.
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.
9.
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:
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.
Page 2
#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 199309L
#include<iostream>
#include<unistd.h>
int main()
{
using namespace std;
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
Page 3
OUTPUT
Number of Clock Tick:100
Maximum Number of Child Process that process can create:999
Maximum Path Length:4096
Maximum No.of Character in a filename:255
Maximum Number of opened files per process:1024
Page 4
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<iostream>
#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;
}
Prepared By: Darshan.K.R, Dept.of CS&E
Page 5
OUTPUT
System
System
System
System
System
Supports
Supports
Supports
Supports
Supports
Page 6
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
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<errno.h>
<fcntl.h>
<unistd.h>
l_len
100,
l_pid*/
0 };
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;
Prepared By: Darshan.K.R, Dept.of CS&E
Page 7
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
Page 8
<fcntl.h>
<sys/stat.h>
<sys/types.h>
<unistd.h>
int main()
{
int fd;
char * myfifo = "/tmp/myfifo"; /* create the FIFO (named pipe) */
mkfifo(myfifo, 0666);
printf("Run Reader process to read the FIFO File\n");
fd = open(myfifo, O_WRONLY);
write(fd,"Hi", sizeof("Hi"));
/* write "Hi" to the FIFO */
close(fd);
unlink(myfifo); /* remove the FIFO */
return 0;
}
[root@localhost /]# gedit reader.c
Page 9
OUTPUT
Received: Hi
Page 10
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);
}
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
Page 11
#include<iostream>
#include<unistd.h>
int main(int argc,char* argv[])
{
using namespace std;
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
Files have been Linked
[root@localhost /]# gedit newfilename
be Copied to newfilename)
Page 12
OUTPUT
output from child
output from parent
[root@localhost /]# ./a.out
output from cohuitlpdu
t from parent
[root@localhost /]# ./a.out
oouuttppuutt ffrroomm pcahrieldnt
Prepared By: Darshan.K.R, Dept.of CS&E
Page 13
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;
}
OUTPUT
PID
1
2
3
4
5
6
7
8
9
10
3087
3088
3166
3167
3168
//Zombie Process
Page 14
pid;
OUTPUT
Second Child,parent pid=1
Page 15
*/
*/
*/
*/
return(status);
}
int main()
{
int
status;
Page 16
OUTPUT
Sun Dec 30 08:38:10 IST 2012
root
pts/0
2012-12-30 08:34 (:0.0)
Page 17
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
Prepared By: Darshan.K.R, Dept.of CS&E
Page 18
Page 19
Page 20
OUTPUT
format of if statement
example............
if(a<b)then(s,a);
if(a<b)then(s,a) else (s,b);
enter the statement
if(a<b)then(s,a);
parsing the input statement...
if (a<b) goto 101
goto 102
101:(s,a)
102:
Page 21
12.
Write a yacc program that accepts a regular expression as input and produce its parse
tree as output
[root@localhost /]# gedit parse.y
%{
#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);
}
void push(char stack[],int *top, char ch)
{
stack[++(*top)]=ch;
}
Page 22
Page 23
OUTPUT
Enter the grammar:(a*b)+c-(d*e)
ab*c+de*-
Page 24
VIVA Question
1.
2. Explain feature test macros. Write a C/C++ POSIX compliant program that is
supported on any given system using feature test macros.
3. Explain the common characteristics of API and describe the error status codes.
4. Describe the characteristics of POSIX.1 FIPS standard and X/Open standard.
5. Differentiate between ANSI C and C++ standards
6. Explain the different file types available in UNIX and POSIX systems
7. Differentiate between hard links and symbolic links with an example.
8. Describe Inode table entries
9. Write a C/C++ program to emulate the UNIX mv command
10. Explain how fcntl API is used for file and record locking.
11. Write the code segment in C that reads utmost 100 bytes into a variable but from
standard input.
12. List and explain the access mode flags and access modifier flags. Also explain how
the permission value specified in an open call is modified by its calling process
umask value.
13. Explain the process of changing user and group ID of files
14. What are named pipes? Explain with an example the use of lseek, link, access with
their prototypes and argument values.
15. Describe the structure of stat system call along with declarations. Also write struct stat
structure.
16. Write a C program to implement the UNIX chown functions
17. Explain Open, Creat, read and fcntl APIs with example
18. With an example program, explain the use of setjmp and longjmp functions.
19. Explain how a C program can be started and various ways of termination.
20. Briefly explain the memory layout of a C program
21. What is fork and vfork? Explain with an example program for each.
22. What is a zombie process?
23. How does UNIX operating system keep process accounting? Write a program to
generate accounting data and give its process structure.
24. Write a C/C++ program to obtain process attributes.
25. Explain wait, waitpid, wait3 and wait4 functions with their prototypes and uses
Page 25
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 IDs of calling
process
Page 26