Lesson 5 Lab
Lesson 5 Lab
Lesson 5 Lab
Overview:
This lab ensures the learning and understanding of concepts such as I/O, pipes, makefile,
and libraries.
After performing the lab you will be able to:
1. Describe input/output streams
2. Discuss inter-process communication using pipes
3. Use named pipes
4. Discuss makefile
5. Create and use static and dynamic libraries
Business Scenario:
The user needs to work on different file-related functionalities which they will achieve by
understanding and using the programs mentioned in the lab.
Detailed Instructions:
Using Input/Output Stream
1. Let us see a scenario where you write a program for reading and writing a students
array in the binary operation mode.
2. The code is as below:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
if(out){
printf("\n-------------------------------------------------------------\n\n");
printf("Writing %d students to file %s\n", size, OUTFILE);
if(fwrite(students, sizeof(Student), size, out) != size)
printf("Error writing students to file\n");
else
printf("Successfully save %d students to file %s\n", size, OUTFILE);
fclose(out);
printf("\n-------------------------------------------------------------\n\n");
}
}
if(in){
printf("\n-------------------------------------------------------------\n\n");
// Get the size of file
fseek(in, 0L, SEEK_END); // Move the file pointer to the end of file
int pos = ftell(in); // Get the current pointer position in the file
rewind(in); // Reset the file pointer to begining of the file
int numStudents = pos / sizeof(Student); // Divide the position with
size of student to get count
if(students){
*outPtr = students;
if(fread(students, sizeof(Student), numStudents, in) != numStudents)
printf("Error reading students from file %s\n", OUTFILE);
else
printf("Successfully read %d students from file %s\n",
numStudents, OUTFILE);
}
fclose(in);
printf("\n-------------------------------------------------------------\n\n");
}
}
Module 5: Plug Things Together
int main(void){
int size = 0;
int index = 0;
float sum = 0.0f;
3. To compile and run the program, copy the source code into a .c file and compile it
using the compilation command.
4. Once the program is compiled successfully, execute the program.
Module 5: Plug Things Together
5. The output of the program is:
-------------------------------------------------------------
-------------------------------------------------------------
-------------------------------------------------------------
-------------------------------------------------------------
int main(void){
int size = 0;
size = sizeof(students)/sizeof(students[0]);
Descriptor pipes[size];
pid_t childPids[size];
Student s;
Student s;
// Read incoming student from child denoted by index
read(pipes[index].parent[READ], &s, sizeof(Student));
printf("Parent %d - Average marks of %s = %.02f\n", getpid(), s.name,
s.averageMarks);
3. To compile and run the program, copy the source code into a .c file and compile it.
4. Once the program is compiled successfully, execute the program.
5. The program returns the below output:
Parent 9293 - Sending student Richard to child process
Child 9294 - Calculating average marks of Richard
Parent 9293 - Average marks of Richard = 81.97
Parent 9293 - Sending student Fred to child process
Child 9295 - Calculating average marks of Fred
Parent 9293 - Average marks of Fred = 48.70
Parent 9293 - Sending student Carl to child process
Child 9296 - Calculating average marks of Carl
Parent 9293 - Average marks of Carl = 70.67
Parent 9293 - Sending student Alex to child process
Child 9297 - Calculating average marks of Alex
Parent 9293 - Average marks of Alex = 68.40
Parent 9293 - Sending student Stacy to child process
Module 5: Plug Things Together
Child 9298 - Calculating average marks of Stacy
Parent 9293 - Average marks of Stacy = 66.27
Module 5: Plug Things Together
Using named pipes
1. In this lab, we will walk you through a program which performs data sharing using
named pipe across processes.
2. The code for this objective is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
void runServer(void){
int sharedFile;
printf("Server started...\n");
// mkfifo(<pathname>,<permission>)
mkfifo(FIFOFILE, 0666);
char msg[80];
while(1)
{
// Server waits for incoming data
sharedFile = open(FIFOFILE,O_RDONLY);
read(sharedFile, msg, 80);
close(sharedFile);
void runClient(void){
int sharedFile;
// mkfifo(<pathname>, <permission>)
mkfifo(FIFOFILE, 0666);
Module 5: Plug Things Together
char msg[80];
while (1)
{
// Open FIFO for write only
sharedFile = open(FIFOFILE, O_WRONLY);
if(strcasecmp(argv[1], "server") == 0)
runServer();
else if(strcasecmp(argv[1], "client") == 0)
runClient();
else
printf("Invalid argument supplied\n");
return 0;
}
3. To compile and run the program, copy the source code into a .c file and compile it.
Module 5: Plug Things Together
4. Once the program is compiled successfully, execute the program using the
respective command.
5. The program has two parts – a server part that can be executed using ‘server’ as
command line argument, and client that is executed using ‘client’ as command line
argument.
6. The output returned is:
Run as server: <executable name> server
Run as client: <executable name> client
Module 5: Plug Things Together
Makefile
1. This lab helps you in understanding makefile and writing one for compiling
programs.
2. The program comprises of three C files and a makefile that need to be copied in a
single directory to perform compilation.
3. Create a directory named include in the current directory.
4. Save the contents of math_utils.h inside the directory.
5. The program code is as below:
math_utils.c
int add(int x, int y){
return (x + y);
}
math_utils.h
int add(int x, int y);
math_user.c
#include<stdio.h>
#include<math_utils.h>
int main(void){
makefile
# Directory structure
# .c files
# makefile
# |--- include
# |--- lib
# |--- bin
# |--- obj
CC=gcc
DEP_HEADERS=math_utils.h
MKDIR_P=mkdir -p
ODIR=./obj
BINDIR=./bin
Module 5: Plug Things Together
IDIR=./include
LDIR=./lib
CFLAGS=-I$(IDIR)
LFLAGS=-L$(LDIR)
LIBS=-lm
directory:
$(MKDIR_P) $(ODIR) $(BINDIR)
6. To compile the program, type the following command at the command prompt from
the current directory which contains two C files and a makefile.
make all
7. You can also try and see the behavior by typing other rules such as directory, clean,
and cleanall.
Module 5: Plug Things Together
Creating and using static library
1. For creating and using a static library, write a program that consists of three files.
2. You have to copy all the three files to a directory and then compile them.
3. The code for creating and using the static library is:
math_utils.c
int add(int x, int y){
return (x + y);
}
math_utils.h
int add(int, int);
math_user.c
#include<stdio.h>
#include<math_utils.h>
int main(void){
4. The generation of a library is a three-step process, where the first step generates an
object file and the second process generates a static library file.
5. The third step consists of compiling the program executable.
6. Type the following command in sequence at the command prompt.
gcc -c math_utils.c
This command will generate an object file math_utils.o
7. Now, type the following command to generate the library libmutila.a
ar rc libmutils.a math_utils.o
The library must have been generated by the above command in the current
directory.
8. Now, generate the executable binary by linking to the library file.
ADD RESULT: 22
Module 5: Plug Things Together
Creating and using Shared/Dynamic library
1. To create and use a shared or dynamic library, first create a program that consists of
three files in total.
2. Copy all the three files to a directory and then compile them.
3. The code for this purpose is given below:
math_utils.c
int add(int x, int y){
return (x + y);
}
math_utils.h
int add(int, int);
math_user.c
#include<stdio.h>
#include<math_utils.h>
int main(void){
4. The generation of a library is a three-step process where the first step generates an
object file, and the second process generates shared library file.
5. The third step includes compiling the program executable.
6. Type the following command in sequence at the command prompt.
gcc –c math_utils.c
The above command would generate an object file math_utils.o
7. Type the following command to generate shared library libmutils.so
gcc -shared math_utils.o -o libmutils.so
The library must be generated by the above command in the current directory.
8. Now, generate the executable binary by linking to the library file.
gcc math_user.c -o math_user –I./ -L./ -lmutils
The above command should generate the binary math_user in the current directory.
9. Execute the binary which should output output as shown below.
ADD RESULT: 22