Linux Programming: Lab Manual
Linux Programming: Lab Manual
Linux Programming: Lab Manual
LAB MANUAL
Prepared By
K. Radhika
Associate Professor
Program Outcomes
PO1 Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals, and
an engineering specialization to the solution of complex engineering problems.
PO2 Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural
sciences, and engineering sciences.
PO3 Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration
for the public health and safety, and the cultural, societal, and environmental considerations.
PO4 Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
PO5 Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding of the limitations.
PO6 The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
PO7 Environment and sustainability: Understand the impact of the professional engineering solutions
in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development.
PO8 Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of
the engineering practice.
PO9 Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
PO10 Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective
reports and design documentation, make effective presentations, and give and receive clear
instructions.
PO11 Project management and finance: Demonstrate knowledge and understanding of the engineering
and management principles and apply these to one’s own work, as a member and leader in a
team, to manage projects and in multidisciplinary environments.
PO12 Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
Program Specific Outcomes
PSO1 Professional Skills: The ability to research, understand and implement computer programs in the areas
related to algorithms, system software, multimedia, web design, big data analytics, and networking for
efficient analysis and design of computer-based systems of varying complexity.
PSO2 Problem-Solving Skills: The ability to apply standard practices and strategies in software project
development using open-ended programming environments to deliver a quality product for business
success.
PSO3 Successful Career and Entrepreneurship: The ability to employ modern computer languages,
environments, and platforms in creating innovative career paths, to be an entrepreneur, and a zest for
higher studies.
LINUX PROGRAMMING LAB SYLLABUS
LINUX PROGRAMMING
1 a) Write a shell script that accepts a file name, starting and ending line numbers as 5
arguments and displays all the lines between the given line numbers.
b) *Illustrate by writing script that will print, message “Hello World, in Bold and
Blink effect, and in different colors like red, brown etc using echo commands?
2 a) Write a shell script that deletes all lines containing a specified word in one or 7
more files supplied as arguments to it.
b) *Illustrate by writing script using for loop to print the following patterns?
i) * ii) 1
** 22
*** 333
**** 4444
***** 55555
3 a) Write a shell script that displays a list of all the files in the current directory to 9
which the user has read, write and execute permissions.
b) * Illustrate to redirect the standard input (stdin) and the standard output (stdout)
of a process, so that scanf () reads from the pipe and printf () writes into the
pipe?
4 a) Write a shell script that receives any number of file names as arguments checks 11
if every argument supplied is a file or a directory and reports accordingly.
Whenever the argument is a file, the number of lines on it is also reported.
b) *Illustrate by writing c program where process forks to a child, and create a
child process by using forks and suddenly terminates itself?
5 Write a shell script that accepts a list of file names as its arguments, counts and 14
reports the occurrence of each word that is present in the first argument file on other
argument files
6 Write a shell script to list all of the directory files in a directory. 15
7 Write a shell script to find factorial of a given integer. 16
8 Write an awk script to count the number of lines in a file that do not contain vowels. 17
9 Write an awk script to find the number of characters, words and lines in a file. 18
10 Write a c program that makes a copy of a file using standard I/O and system calls 19
11 Implement in C the following UNIX commands using System calls 20
a) cat b) ls c) mv
12 Write a program that takes one or more file/directory names as command line input 23
and reports the following information on the file.
a) File type b) Number of links c) Time of last access
d) Read Write and Execute permissions
13 Write a C program to emulate the UNIX ls –l command. 24
14 Write a C program to list for every file in a directory, its inode number and file 25
name.
15 Write a C program that demonstrates redirection of standard output to a file.Ex: ls > 26
f1.
S. No. List of Experiments Page No.
16 Write a C program to create a child process and allow the parent to display “parent” 27
and the child to display “child” on the screen.
17 Write a C program to create a Zombie process. 28
18 Write a C program that illustrates how an orphan is created. 29
19 Write a C program that illustrates how to execute two commands concurrently with 30
a command pipe. Ex: - ls –l | sort
20 Write C programs that illustrate communication between two unrelated processes 33
using named pipe.
21 Write a C program to create a message queue with read and write permissions to 34
write 3 messages to it with different priority numbers.
22 Write a C program that receives the messages (from the above message queue as 36
specified in (21)) and displays them.
23 Write a C program to allow cooperating processes to lock a resource for exclusive 38
use, using
a) Semaphores b) flock or lockf system calls.
24 Write a C program that illustrates suspending and resuming processes using signals 39
25 Write a C program that implements a producer-consumer system with two 40
processes.
26 Write client and server programs (using c) for interaction between server and client 41
processes using Unix Domain sockets. (Using Semaphores).
27 Write client and server programs (using c) for interaction between server and client 45
processes using Internet Domain sockets.
28 Write a C program that illustrates two processes communicating using shared 48
memory.
ATTAINMENT OF PROGRAM OUTCOMES
& PROGRAM SPECIFIC OUTCOMES
Program
Program
Exp. Specific
Experiment Outcomes
No. Attained Outcomes
Attained
LINUX PROGRAMMING
1 c) Write a shell script that accepts a file name, starting and ending line
numbers as arguments and displays all the lines between the given
line numbers.
PO1, PO2 PSO1
d) *Illustrate by writing script that will print, message “Hello World, in
Bold and Blink effect, and in different colors like red, brown etc
using echo commands?
2 c) Write a shell script that deletes all lines containing a specified word
in one or more files supplied as arguments to it.
d) *Illustrate by writing script using for loop to print the following
patterns?
i) * ii) 1 PO1 PSO1
** 22
*** 333
**** 4444
***** 55555
3 c) Write a shell script that displays a list of all the files in the current
directory to which the user has read, write and execute permissions.
d) * Illustrate to redirect the standard input (stdin) and the standard PO1, PO2 PSO1
output (stdout) of a process, so that scanf () reads from the pipe and
printf () writes into the pipe?
4 c) Write a shell script that receives any number of file names as
arguments checks if every argument supplied is a file or a directory
and reports accordingly. Whenever the argument is a file, the
PO1 PSO1
number of lines on it is also reported.
d) *Illustrate by writing c program where process forks to a child, and
create a child process by using forks and suddenly terminates itself?
5 Write a shell script that accepts a list of file names as its arguments,
counts and reports the occurrence of each word that is present in the first PO1 PSO1
argument file on other argument files.
6 Write a shell script to list all of the directory files in a directory. PO1 PSO1
7 Write a shell script to find factorial of a given integer. PO1 PSO1
8 Write an awk script to count the number of lines in a file that do not
PO1 PSO1
contain vowels.
9 Write an awk script to find the number of characters, words and lines in
PO1 PSO1
a file.
10 Write a c program that makes a copy of a file using standard I/O and
PO1 PSO1
system calls
Program
Program
Exp. Specific
Experiment Outcomes
No. Attained Outcomes
Attained
11 Implement in C the following UNIX commands using System calls
PO1, PO2 PSO1
a) cat b) ls c) mv
12 Write a program that takes one or more file/directory names as
command line input and reports the following information on the file.
PO1, PO2 PSO1
a) File type b) Number of links c) Time of last access
d) Read Write and Execute permissions
13 Write a C program to emulate the UNIX ls –l command. PO1 PSO1
14 Write a C program to list for every file in a directory, its inode number
PO1 PSO1
and file name.
15 Write a C program that demonstrates redirection of standard output to a
PO1 PSO1
file.Ex: ls > f1.
16 Write a C program to create a child process and allow the parent to
PO1 PSO1
display “parent” and the child to display “child” on the screen.
17 Write a C program to create a Zombie process. PO1 PSO1
18 Write a C program that illustrates how an orphan is created. PO1 PSO1
19 Write a C program that illustrates how to execute two commands
PO1 PSO1
concurrently with a command pipe. Ex: - ls –l | sort
20 Write C programs that illustrate communication between two unrelated
PO1 PSO1
processes using named pipe.
21 Write a C program to create a message queue with read and write
PO1 PSO1
permissions to write 3 messages to it with different priority numbers.
22 Write a C program that receives the messages (from the above message
PO1 PSO1
queue as specified in (21)) and displays them.
23 Write a C program to allow cooperating processes to lock a resource for
exclusive use, using PO1, PO2 PSO1
a) Semaphores b) flock or lockf system calls.
24 Write a C program that illustrates suspending and resuming processes
PO1, PO2 PSO1
using signals
25 Write a C program that implements a producer-consumer system with PO1, PO2, PSO1,
two processes. PO4 PSO2
26 Write client and server programs (using c) for interaction between
server and client processes using Unix Domain sockets. (Using PO1, PO2, PSO1,
PO3, PO4 PSO2
Semaphores).
27 Write client and server programs (using c) for interaction between PO1, PO2, PSO1,
server and client processes using Internet Domain sockets. PO3, PO4 PSO2
28 Write a C program that illustrates two processes communicating using PO1, PO2, PSO1,
shared memory. PO3, PO4 PSO2
LINUX PROGRAMMING LABORATORY
OBJECTIVE:
The Linux programming laboratory course covers major methods of Inter Process Communication
(IPC), which is the basis of all client / server applications under Linux, Linux Utilities, working
with the Bourne again shell (bash), files, process and signals. There will be extensive programming
exercises in shell scripts. It also emphasizes various concepts in multithreaded programming and
socket programming.
Data mining tools allow predicting future trends and behaviors, allowing businesses to make
proactive, knowledge-driven decisions. The data mining laboratory course is designed to exercise
the data mining techniques such as classification, clustering, pattern mining etc with varied datasets
and dynamic parameters. Weka data mining tool is used for the purpose of acquainting the students
with the basic environment of the data mining tools.
OUTCOMES:
Upon the completion of Linux Programming and Data Mining practical course, the student will be able
to:
1.1 OBJECTIVE
a) Write a shell script that accepts a file name, starting and ending line numbers as arguments
and displays all the lines between the given line numbers.
b) *Illustrate by writing script that will print, message “Hello World, in Bold and Blink effect,
and in different colors like red, brown etc using echo commands?
1.2 RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
5
echo -e "\033[31m Hello World"
#print in Red color
echo -e "\033[32m Hello World"
# Green color
echo -e "\033[33m Hello World"
# See remains on screen
echo -e "\033[34m Hello World"
echo -e "\033[35m Hello World"
echo -e "\033[36m Hello World"
echo -e -n "\033[0m "
# print back to normal
echo -e "\033[41m Hello World"
echo -e "\033[42m Hello World"
echo -e "\033[43m Hello World"
echo -e "\033[44m Hello World"
echo -e "\033[45m Hello World"
echo -e "\033[46m Hello World"
echo -e "\033[0m Hello World"
# Print back to normal
6
EXPERIMENT 2
2.1 OBJECTIVE
a) Write a shell script that deletes all lines containing a specified word in one or more
files supplied as arguments to it.
b) *Illustrate by writing script using for loop to print the following patterns?
2.2 RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
INPUT:
sh prog2.sh 3.sh
enter the word
echo
OUTPUT:
The given input filename is : 3.sh
It displays all the lines other than pattern matching
7
do
echo -n " *"
done
echo ""
done
#
8
EXPERIMENT 3
3.1 OBJECTIVE
a) Write a shell script that displays a list of all the files in the current directory to which the user has read,
write and execute permissions.
b) Illustrate to redirect the standard input (stdin) and the standard output (stdout) of a process, so
that scanf () reads from the pipe and printf () writes into the pipe?
3.2 RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
10
EXPERIMENT 4
4.1 OBJECTIVE
a) Write a shell script that receives any number of file names as arguments checks if every argument
supplied is a file or a directory and reports accordingly. Whenever the argument is a file, the number
of lines on it is also reported.
b) *Illustrate by writing c program where process forks to a child, and create a child process by using
forks and suddenly terminates itself?
4.2 RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
INPUT: sh
prog4.sh
OUTPUT:
enter the name
file 3.sh
number of lines 9
11
if ( childpid == -1 ) {
perror("Cannot proceed. fork() error");
return 1;
}
if (childpid == 0) {
printf("Child 1: I inherited my parent's pid as %d.\n", mypid);
mypid = getpid();
printf("Child 1: getppid() tells my parent is %d. My own pid instead is %d.\n", getppid(), mypid);
/* forks another child */
childpid = fork();
if ( childpid == -1 ) {
perror("Cannot proceed. fork() error");
return 1;
}
if (childpid == 0) {
/* this is the child of the first child, thus "Child 2" */
printf("Child 2: I hinerited my parent's PID as %d.\n", mypid);
mypid = getpid();
printf("Child 2: getppid() tells my parent is %d. My own pid instead is %d.\n", getppid(),
mypid);
childpid = fork();
if ( childpid == -1 ) {
perror("Cannot proceed. fork() error");
return 1;
}
if (childpid == 0) {
/* "Child 3" sleeps 30 seconds then terminates 12, hopefully before its parent "Child 2" */
printf("Child 3: I hinerited my parent's PID as %d.\n", mypid);
mypid = getpid();
printf("Child 3: getppid() tells my parent is %d. My own pid instead is %d.\n", getppid(),
mypid);
sleep(30);
return 12;
} else /* the parent "Child 2" suddendly returns 15 */ return 15; }
else {
/* this is still "Child 1", which waits for its child to exit */
while ( waitpid(childpid, &status, WNOHANG) == 0 ) sleep(1);
if ( WIFEXITED(status) ) printf("Child1: Child 2 exited with exit status %d.\n",
WEXITSTATUS(status));
else printf("Child 1: child has not terminated correctly.\n");
}
} else {
/* then we're the parent process, "Parent" */
printf("Parent: fork() went ok. My child's PID is %d\n",
childpid); /* wait for the child to terminate and report about that
*/ wait(&status);
if ( WIFEXITED(status) ) printf("Parent: child has exited with status %d.\n",
WEXITSTATUS(status));
else printf("Parent: child has not terminated normally.\n");
}
return 0;
}
12
4.5 PRE-LAB QUESTIONS
1. How to write arithmetic multiplication operator in shell.
2. Write down the syntax for nested if statement.
13
EXPERIMENT 5
5.1 OBJECTIVE
Write a shell script that accepts a list of file names as its arguments, counts and reports the occurrence
of each word that is present in the first argument file on other argument files.
5.2 RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
14
EXPERIMENT 6
6.1 OBJECTIVE
Write a shell script to list all of the directory files in a directory.
6.2 RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
INPUT:
sh Lp6.sh
Enter dir name
Presanna
OUTPUT:
Files in prasanna
are 3.sh
4.sh
pp2.txt
15
EXPERIMENT 7
7.1 OBJECTIVE
Write a shell script to find factorial of a given number.
7.2 RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
16
EXPERIMENT 8
8.1 OBJECTIVE
Write an awk script to count the number of lines in a file that do not contain vowels.
8.2 RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
INPUT:
awk prog8.awk lp1.sh
Displaying number of lines in a file that do not contain vowels
OUTPUT:
The total lines in a file that do not contain vowels:1
17
EXPERIMENT 9
9.1 OBJECTIVE
Write an awk script to find the number of characters, words and lines in a file.
9.2 RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
INPUT:
awk prog9.awk lp5.sh
OUTPUT:
The total number of characters, words and lines in a file
is: Words:12
Lines:3
Chars:39
18
EXPERIMENT 10
10.1 OBJECTIVE
Write a C program that makes a copy of a file using Systems calls
10.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, shell interpreter
INPUT: cc
prog10.c
./a.out
entr the source file name:
file1
enter the destination file
name: file2
OUTPUT:
The copy of a file is successes
11.A.1 OBJECTIVE
Write a C Program to Implement the Unix command cat using system calls.
11.A.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, shell interpreter
INPUT:
cc prog11a.c unit1
OUTPUT:
Displays content of file
11.B.1 OBJECTIVE
Write a C Program to Implement the Unix command ls using system calls
20
11. B.3 PROGRAM LOGIC
1. Open a directory of given directory name
2. Scan directory and Read file and display filename to output stream
3. Repeat step 2 till eof directory reach.
INPUT:
Cc prog11c.c
OUTPUT:
Current Working Directory =/home/prasanna
Number of files:2
Lp1.sh
lp2.sh
21
11. B.6 LAB ASSIGNMENT
1. Write a c-program to reverse a file using lseek();
11.C.1 OBJECTIVE
Write a C Program to Implement the Unix command mv using system calls.
INPUT:
cc mv.c file1 file2
OUTPUT:
# creates file2 and copies the content of file1 to file2 and removes file1
22
EXPERIMENT 12
12.1 OBJECTIVE
Write a C program that takes one or more file or directory names as command line input and
reports the following information on the file.
1. file type
2. number of links
3. read, write and execute permissions
4. time of last access
(Note: use /fstat system calls)
12.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
23
EXPERIMENT 13
13.1 OBJECTIVE
Write a C program to emulate the Unix ls – l command.
13.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
24
EXPERIMENT 14
14.1 OBJECTIVE
Write a C program to list for every file in a directory, its inode number and file name
14.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
INPUT:
cc inode.c –o inode
./inode
OUTPUT:
FILE NAME INODE NUMBER
………….. 4195164
File2.c 4195164
….
File1.c 4195164
25
EXPERIMENT 15
15.1 OBJECTIVE
Write a C program that demonstrates redirection of standard output to a file. Ex: ls >f1.
/* freopen example: redirecting stdout */
15.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
INPUT:
cc 15.c –o file1
./file1
OUTPUT:
This sentence is redirected to a file which is given at output
26
EXPERIMENT 16
16.1 OBJECTIVE
Write a C program to create a child process and allow the parent to display “parent” and the
child to display “child” on the screen.
16.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
INPUT:
$cc fork.c ... run the program.
./a.out
OUTPUT:
I'm the original process with PID 13292 and PPID 13273.
I'm the parent process with PID 13292 and PPID 13273.
My child's PID is 13293.
I'm the child process with PID 13293 and PPID 13292.
PID 13293 terminates. ... child terminates.
PID 13292 terminates. ... parent terminates.
17.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
INPUT:
$ cc prog17.c
./a.out& ... execute the program in the background.
[1] 13545
OUTPUT:
$ ps
PID TT STAT TIME COMMAND
13535 p2 s 0:00 -ksh(ksh) ... the shell
13545 p2 s 0:00 aombie.exe... the parent process
13536 p2 z 0:00 <defunct> ... the zombie child process
13537 p2 R 0:00 ps
$ kill 13545 ... kill the parent process.
[1] Terminated zombie.exe
$ ps ... notice the zombie is gone now.
PID TT STAT TIME COMMAND
13535 p2 s 0:00 -csh(csh)
13548 p2 R 0:00 ps
28
EXPERIMENT 18
18.1 OBJECTIVE
Write a C program that illustrates how an orphan is created.
18.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
INPUT:
$cc prog18.c
./a.out ... run the program.
OUTPUT:
I'm the original process with PID 13364 and PPID 13346.
I'm the parent process with PID 13364 and PPID 13346.
PID 13364 terminates.
I'm the child process with PID 13365 and PPID 1. ...orphaned!
PID 13365 terminates. ... child terminates.
19.1 OBJECTIVE
Write a C program that illustrates how to execute two commands concurrently with a command
pipe. Eg. ls-l|sort.
19.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
30
break;
case S_IFCHR: printf(" c");
break;
case S_IFBLK: printf(" b");
break;
case S_IFLNK: printf(" l");
break;
case S_IFSOCK: printf("
s"); break;
case S_IFIFO: printf(" p");
break;
}
if(S_IRUSR & b.st_mode)
printf(" r");
else
printf(" -"); if(S_IWUSR
& b.st_mode) printf(" w");
else
printf(" -");
if(S_IXUSR & b.st_mode)
printf(" x");
else
printf(" -");
if(S_IRGRP & b.st_mode)
printf(" r");
else
printf(" -");
if(S_IWGRP & b.st_mode)
printf(" w");
else
printf(" -");
if(S_IXGRP & b.st_mode)
printf(" x");
else
printf(" -");
if(S_IROTH & b.st_mode)
printf(" r");
else
printf(" -");
if(S_IWOTH & b.st_mode)
printf(" w");
else
printf(" -");
if(S_IXOTH & b.st_mode)
printf(" x");
else
printf(" -");
printf("%3d ",b.st_nlink);
printf("%4d ",b.st_uid);
printf("%4d ",b.st_gid);
printf("%6d ",b.st_size);
printf("%9ld",b.st_ctime);
31
printf(" %s\n",p->d_name);
}
}
INPUT:
vi 19.c
cc 19.c
./a.out
OUTPUT:
- r w - r w - r - - 1 500 500 1506 1380610351 19.c
- r w - r w - r - - 1 500 500 0 1380523478 2
- r w - r w - r - - 1 500 500 0 1380523478 3
- r w x r w x r - x 1 500 500 6038 1380610357 a.out
- r w - r w - r - - 1 500 500 0 1380523478 1
- r w - r w - r - - 1 500 500 421 1380524812 12.c
- r w - r w - r - - 1 500 500 0 1380523478 4
d r w x - - - - - - 15 500 500 4096 1380609957 ..
d r w x r w x r - x 2 500 500 4096 1380610357 .
- r w - r w - r - - 1 500 500 347 1380523684 13.c
32
EXPERIMENT 20
20.1 OBJECTIVE
Write a C program in which a parent writes a message to a pipe and the child reads the message.
20.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
OUTPUT:
CHILD: Writing to the pipe
CHILD:Exiting
PARENT:reading from the pipe
PARENT:Received Data is : Hello World, I am child
33
EXPERIMENT 21
21.1 OBJECTIVE
Write a C program (sender.c) to create a message queue with read and write permissions to write 3
messages to it with different priority numbers.
21.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
{
perror("message failed\n");
exit(1);
}
}
34
}
INPUT: cc
prog21.c
OUTPUT:
Enter the message to send: hi
Enter the message to send: hello, how are you
Enter the message to send: bye
35
EXPERIMENT 22
22.1 OBJECTIVE
Write a C program (receiver.c) that receives the messages (from the above message queue
as specified in (22)) and displays them.
22.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
36
printf("Message received from sender is %s\n",buff.mtext);
}
}
INPUT:
cc prog22.c
./a.out
OUTPUT:
Message received from sender is: hi
Message received from sender is: hello, how are you
Message received from sender is: bye
37
EXPERIMENT 23
23.1 OBJECTIVE
Write a C program to allow cooperating processes to lock a resource for exclusive use, using
a) Semaphores b) flock or lockf system calls.
23.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
38
EXPERIMENT 24
24.1 OBJECTIVE
Write a C program that illustrates suspending and resuming processes using signals.
24.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
39
EXPERIMENT 25
25.1 OBJECTIVE
Write a C program that implements a producer-consumer system with two processes. (using
Semaphores).
25.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
40
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
printf(“producer:‟%d‟\n”,i);
fflush(stdout);
}
break;
default:
for(i=0;i<num_loops;i++)
{
printf(“consumer:‟%d‟\n”,i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
if(rand()>3*(rano_max14));
{
delay.tv_sec=0;
delay.tv_nsec=10;
nanosleep(&delay,null);
}
}
break;
}
return 0;
}
41
EXPERIMENT 26
26.1 OBJECTIVE
Write client and server programs (using c) for interaction between server and client processes
using Unix Domain sockets.
26.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
close(connection_fd);
return 0;
}
int main(void)
{
struct sockaddr_un address;
int socket_fd, connection_fd;
socklen_t address_length;
pid_t child;
42
}
unlink("./demo_socket");
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
if(bind(socket_fd,
(struct sockaddr *) &address,
sizeof(struct sockaddr_un)) != 0)
{
printf("bind()
failed\n"); return 1;
}
if(listen(socket_fd, 5) != 0)
{
printf("listen()
failed\n"); return 1;
}
close(socket_fd);
unlink("./demo_socket");
return 0;
}
Client.c
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
43
struct sockaddr_un address;
int socket_fd, nbytes;
char buffer[256];
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
if(connect(socket_fd,
(struct sockaddr *) &address,
sizeof(struct sockaddr_un)) != 0)
{
printf("connect() failed\n");
return 1;
}
44
EXPERIMENT 27
27.1 OBJECTIVE
Write client and server programs (using c) for interaction between server and client processes
using Internet Domain sockets.
27.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
Server.c
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
char sendBuff[1025];
time_t ticks;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
45
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n",
ctime(&ticks)); write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(1);
}
}
Client.c
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket
\n"); return 1;
}
46
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(n < 0)
{
printf("\n Read error \n");
}
return 0;
}
27.5 PRE-LAB QUESTIONS
1. Explain about IPV6 socket address structure and compare it with IPV4 and unix socket address
structures.
47
EXPERIMENT 28
28.1 OBJECTIVE
Implement shared memory form of IPC
28.2 RESOURCE/REQUIREMENTS
Linux operating system, vi –editor, c-compiler
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
main()
{ char c; int
shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0)
{ perror("shmget");
exit(1); }
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
{ perror("shmat");
exit(1); } s
= shm;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = NULL;
while (*shm Linux operating system, vi –editor, c-compiler= '*')
sleep(1);
exit(0);
}
shm_client.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
main()
{
int shmid;
key_t key;
48
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
perror("shmget");
exit(1); }
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1); }
for (s = shm; *s != NULL; s++)
putchar(*s);
putchar('\n');
*shm = '*';
exit(0);
}
49