Linux<br />12 January 2010<br />Knowx innovation<br />1<br />
Unix<br />Developed at Bell Labs in late 1960s<br />Owned by AT&T at the time, which meant you had to buy it once AT&T fig...
Why Linux<br />Entire OS source code is free<br />Full freedom to study, modify, redistribute. No payments or restrictions...
Protected memory<br />True multiuser capabilities<br />Filesystem choices:ext3, JFS, XFS, reiserFS<br />Highly modular & s...
distribution<br /><ul><li>Debian (related: Knoppix, (k)ubuntu, Mepis, Xandros)
RedHat (related: Fedora, CentOS)
SUSE (related: OpenSUSE)
Mandriva
Slackware
Embedded Linuxes</li></ul>12 January 2010<br />Knowx innovation<br />5<br />
GNU/Linux architecture<br />12 January 2010<br />Knowx innovation<br />6<br />
Linux kernel<br />Mediates access to the hardware and peripherals<br />Shells <br />Provides user access to the kernel<br ...
Kernel architecture<br />12 January 2010<br />Knowx innovation<br />8<br />
Gnu compiler collection(gcc)<br />Compiler and set of utilities to build binaries from high level source code.<br />Standa...
Stage of compilation<br />12 January 2010<br />Knowx innovation<br />10<br />
12 January 2010<br />Knowx innovation<br />11<br />
makefile<br />12 January 2010<br />Knowx innovation<br />12<br />
make utility uses a developer-created input file to describe the project built<br />GNU uses the name Makefile as the defa...
appexp : main.o app.o bar.o lib.o<br />	gcc –o appexp main.o app.o bar.o lib.o<br />main.o : main.c lib.h app.h<br />	gcc ...
File handling<br />Accomplished through the standard C library<br />We can create and manipulate ASCII text or binary file...
fopen <br />Opening a file can also be the mechanism to create a file<br />prototype:<br />FILE * fopen(const char *filena...
Example<br />FILE *fin; <br />fin = fopen(&quot;inpfile.txt&quot;, &quot;r&quot;); <br />12 January 2010<br />Knowx innova...
Program<br />#include &lt;stdio.h&gt; <br />#define MYFILE &quot;missing.txt&quot; <br />main() <br />{<br />FILE *fin;	 <...
fread()  and  fwrite()<br />Prototypes:<br />size_t fread(void *ptr, size_t size, size_t nmemb,FILE *stream);<br />size_t ...
SEEK_SET   -   moves the file position to the position defined by 			offset<br />SEEK_CUR  -moves the file position the nu...
Program<br />#include &lt;stdio.h&gt;<br />#define MAX_LINE 40 <br />#define FILENAME &quot;myfile.txt“<br />typedef struc...
 if (fin == (FILE *)0) <br />	exit(-1);<br />fseek( fin, (2 * sizeof(MY_TYPE_T)), SEEK_SET ); /* Get the last entry */<br ...
/* Get the first entry */<br />rewind( fin ); <br />fread( &object, sizeof(MY_TYPE_T), 1, fin ); <br />printf(&quot;%d %f ...
getpid()    - to get the current process ID.<br />getppid()  - to get the parent process ID.<br />getuid()    - to get the...
Program<br />#include &lt;stdio.h&gt; <br />#include &lt;unistd.h&gt; <br />#include &lt;sys/types.h&gt; <br />int main()<...
myUid = getuid(); <br />	printf( &quot;my process id is %d
&quot;, myPid ); <br />	printf( &quot;my parent&apos;s process ...
Process api<br />Fork<br />Create a new child process<br />Wait<br />Suspend execution until a child processes exits<br />...
Fork<br />Creating new processes with in a given process.<br />return value of fork is <br />greater than zero then in the...
Example<br />pid_t pid;<br />pid = fork();<br />if(pid&gt;0)<br />	{/*parent context*/}<br />else if (pid ==0)<br />	{/*ch...
12 January 2010<br />Knowx innovation<br />30<br />
Program<br />#include &lt;sys/types.h&gt; <br />#include &lt;sys/wait.h&gt; <br />#include &lt;unistd.h&gt;<br />#include ...
ret = fork();<br /> if (ret &gt; 0)<br /> { 	printf(&quot;Parent: This is the parent process (pid %d)
&quot;, 	getpid());<...
	for (i = 0 ; i &lt; 6 ; i++) <br />	{<br />printf(&quot;Child: At count %d
&quot;, i); <br />	sleep(1); } role = 1;<br />...
wait<br />Suspend the calling process until a child process(created by this process) exits or until a signal is delivered....
Signal<br />Install a signal handler for a process.<br />Prototype<br />sighandler_t signal (int signum, sighandler_t hand...
Program<br />#include &lt;stdio.h&gt; <br />#include &lt;sys/types.h&gt;<br />#include &lt;sys/wait.h&gt;<br />#include &l...
ret = fork();<br />if (ret &gt; 0)<br /> {	 /* Parent Context */<br />printf( &quot;Parent: This is the parent process (pi...
else if (ret == 0) <br />{ /* Child Context */ <br />printf( &quot;Child: This is the child process (pid %d)
&quot;, getpi...
exec<br />Replaces the current process image altogether.<br />Once the exec function replaces the current process, its pid...
Example<br />	execl(“/bin/ls”,”ls”,”-la”,null);<br />This command replaces the current process with the ls (list directory...
Program for shell interpreter<br />#include &lt;sys/types.h&gt; <br />#include &lt;sys/wait.h&gt;<br />#include &lt;unistd...
 while (1) <br />{<br />printf(&quot;mysh&gt;&quot;); <br />sret = fgets( cmd, sizeof(cmd), stdin ); <br />if (sret == NUL...
Message queues<br />Messages are small collections of data (400 bytes, for example) that can be passed between cooperating...
<ul><li>Creating message queue </li></ul>API<br />msgget(key_k key , int msgflg)<br />the key which signifies the name giv...
Program<br />#include &lt;stdio.h&gt; <br />#include &lt;sys/msg.h&gt;<br />#define MY_MQ_ID 	111<br />int main()<br />{<b...
<ul><li>Sending the message</li></ul>API<br />int msgsnd (int msqid, const void *msgp, size_t msgsz, int msgflg); <br />Ta...
MSGSZ is the length of the message sent in bytes.<br />MSGFLG specifies the action to be taken if one or more of the follo...
Program<br />#include &lt;sys/types.h&gt; <br />#include &lt;sys/ipc.h&gt; <br />#include &lt;sys/msg.h&gt;<br />#include ...
/*A queue is created using the msget function with a key value 10 and the flag parameter being IPC_CREAT|06668 */<br /> if...
 buf_length = strlen(sbuf.mtext) + 1 ; <br />/*sending the message with option IPC_NOWAIT */<br />if (msgsnd(msqid, &sbuf,...
<ul><li>Receive the message</li></ul>API<br />int msgrcv (int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);<b...
program<br />#include &lt;sys/types.h&gt; <br />#include &lt;sys/ipc.h&gt; <br />#include &lt;sys/msg.h&gt; <br />#include...
 int msqid; <br />key_t key; <br />message_buf rbuf; <br />key = 10;<br />/*creating a queue with the key value 10, */<br ...
<ul><li>Configuring message queue</li></ul>API<br />msgctl(int msqid, int cmd, struct msqid_ds *buf)<br />3 Arguments <br ...
12 January 2010<br />Knowx innovation<br />55<br />
Variable that is protected.<br />It provides a means to restrict access to a resource that is shared amongst two or more p...
The signal notations<br />P(semaphore variable) for wait<br />V(semaphore variable) for signal<br />12 January 2010<br />K...
A binary semaphore is a variable that can take only the values 0 and 1.<br />Represents a single resource and therefore wh...
12 January 2010<br />Knowx innovation<br />59<br />
Represent shared resources in quantities greater than one.<br />It could represent the entire set of buffers by setting it...
12 January 2010<br />Knowx innovation<br />61<br />
<ul><li>Creating semaphore</li></ul>API<br />semget( key_t key, int num_sems, int sem_flags)<br />Key is the semaphore key...
sem_id, is the semaphore identifier, as returned from semget.<br />sem_ops, is a pointer to an array of structures, each o...
Upcoming SlideShare
Loading in...5
×

Linux

1,237

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,237
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
58
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Linux

  1. 1. Linux<br />12 January 2010<br />Knowx innovation<br />1<br />
  2. 2. Unix<br />Developed at Bell Labs in late 1960s<br />Owned by AT&T at the time, which meant you had to buy it once AT&T figured out it was valuable<br />Written in C (a high level language)<br />Needs to be compiled to run<br />Can be easily moved to a different system by re-compiling for new hardware<br />12 January 2010<br />Knowx innovation<br />2<br />
  3. 3. Why Linux<br />Entire OS source code is free<br />Full freedom to study, modify, redistribute. No payments or restrictions.<br />Kernel and many packages follow GNU GPL, LGPL or BSD style license.<br />Powerful command line (shells)<br />Multitasking, SMP, NUMA<br />12 January 2010<br />Knowx innovation<br />3<br />
  4. 4. Protected memory<br />True multiuser capabilities<br />Filesystem choices:ext3, JFS, XFS, reiserFS<br />Highly modular & scalable: From embedded systems to IBM mainframes <br />12 January 2010<br />Knowx innovation<br />4<br />
  5. 5. distribution<br /><ul><li>Debian (related: Knoppix, (k)ubuntu, Mepis, Xandros)
  6. 6. RedHat (related: Fedora, CentOS)
  7. 7. SUSE (related: OpenSUSE)
  8. 8. Mandriva
  9. 9. Slackware
  10. 10. Embedded Linuxes</li></ul>12 January 2010<br />Knowx innovation<br />5<br />
  11. 11. GNU/Linux architecture<br />12 January 2010<br />Knowx innovation<br />6<br />
  12. 12. Linux kernel<br />Mediates access to the hardware and peripherals<br />Shells <br />Provides user access to the kernel<br />Applications <br />Provide the useful function for the operating system<br />12 January 2010<br />Knowx innovation<br />7<br />
  13. 13. Kernel architecture<br />12 January 2010<br />Knowx innovation<br />8<br />
  14. 14. Gnu compiler collection(gcc)<br />Compiler and set of utilities to build binaries from high level source code.<br />Standard for embedded systems development<br />Since its supports so many different target architectures<br />Supports a number of languages<br />C, C++, ada, java, FORTRAN, Pascal<br />12 January 2010<br />Knowx innovation<br />9<br />
  15. 15. Stage of compilation<br />12 January 2010<br />Knowx innovation<br />10<br />
  16. 16. 12 January 2010<br />Knowx innovation<br />11<br />
  17. 17. makefile<br />12 January 2010<br />Knowx innovation<br />12<br />
  18. 18. make utility uses a developer-created input file to describe the project built<br />GNU uses the name Makefile as the default name for its input file.<br />12 January 2010<br />Knowx innovation<br />13<br />
  19. 19. appexp : main.o app.o bar.o lib.o<br /> gcc –o appexp main.o app.o bar.o lib.o<br />main.o : main.c lib.h app.h<br /> gcc –c -o main.o main.c<br />app.o: app.c lib.h app.h<br /> gcc –c -o app.o app.c<br />bar.o : bar.c lib.h<br /> gcc –c -o bar.o bar.c<br />lib.o : lib.c lib.h<br /> gcc –c -o lib.o lib.c<br />Line 1: is the rule<br />The portion of the rule before the colon is called targetand after the colon is called dependencies.<br />12 January 2010<br />Knowx innovation<br />14<br />
  20. 20. File handling<br />Accomplished through the standard C library<br />We can create and manipulate ASCII text or binary files with the same API.<br />API<br />fopen, fclose, fwrite , fread, fseek, and rewind.<br />12 January 2010<br />Knowx innovation<br />15<br />
  21. 21. fopen <br />Opening a file can also be the mechanism to create a file<br />prototype:<br />FILE * fopen(const char *filename,const char *mode)<br />filename - file we wish to access or create<br />mode – mode we wish to use<br />FILE * - fopen returns the FILE pointer (FILE *) <br />12 January 2010<br />Knowx innovation<br />16<br />
  22. 22. Example<br />FILE *fin; <br />fin = fopen(&quot;inpfile.txt&quot;, &quot;r&quot;); <br />12 January 2010<br />Knowx innovation<br />17<br />
  23. 23. Program<br />#include &lt;stdio.h&gt; <br />#define MYFILE &quot;missing.txt&quot; <br />main() <br />{<br />FILE *fin; <br /> fin = fopen( MYFILE, &quot;r&quot; ); /* Try to open the file for read */ <br /> if (fin == (FILE *)0) /* Check for failure to open */<br /> {<br />printf(“error in opening file”); /* Emit an error message and exit */<br />exit(-1);<br />} <br /> fclose( fin ); /* All was well, close the file */ <br />}<br />12 January 2010<br />Knowx innovation<br />18<br />
  24. 24. fread() and fwrite()<br />Prototypes:<br />size_t fread(void *ptr, size_t size, size_t nmemb,FILE *stream);<br />size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);<br />fseek()<br />Prototype:<br />int fseek (FILE * stream, long offset, int whence);<br />Function allows to the new position given an index<br />whence argument defines <br />12 January 2010<br />Knowx innovation<br />19<br />
  25. 25. SEEK_SET - moves the file position to the position defined by offset<br />SEEK_CUR -moves the file position the number of bytes defined by offset from the current file position<br />SEEK_END - moves the file position to the number of bytes defined by offset from the end of the file<br />rewind ()<br />Resets the file read pointer back to the start of the file<br />Prototype<br />void rewind (FILE * stream);<br />12 January 2010<br />Knowx innovation<br />20<br />
  26. 26. Program<br />#include &lt;stdio.h&gt;<br />#define MAX_LINE 40 <br />#define FILENAME &quot;myfile.txt“<br />typedef struct <br />{ int id; float x_coord; float y_coord; char name[MAX_LINE+1]; <br />}<br />MY_TYPE_T; <br />MY_TYPE_T object;<br />int main()<br /> {<br />int i;<br />FILE *fin; <br />fin = fopen( FILENAME, &quot;r&quot; ); /* Open the input file */<br />12 January 2010<br />Knowx innovation<br />21<br />
  27. 27. if (fin == (FILE *)0) <br /> exit(-1);<br />fseek( fin, (2 * sizeof(MY_TYPE_T)), SEEK_SET ); /* Get the last entry */<br />fread( &object, sizeof(MY_TYPE_T), 1, fin ); <br />printf(&quot;%d %f %f %s &quot;, object.id, object.x_coord, object.y_coord, object.name );<br />rewind( fin ); /* Get the second to last entry */ <br />fseek( fin, (1 * sizeof(MY_TYPE_T)), SEEK_SET );<br />fread( &object, sizeof(MY_TYPE_T), 1, fin );<br />printf(&quot;%d %f %f %s &quot;, object.id, object.x_coord, object.y_coord, object.name ); <br />12 January 2010<br />Knowx innovation<br />22<br />
  28. 28. /* Get the first entry */<br />rewind( fin ); <br />fread( &object, sizeof(MY_TYPE_T), 1, fin ); <br />printf(&quot;%d %f %f %s &quot;, object.id, object.x_coord, object.y_coord, object.name ); fclose( fin );<br />return 0; <br />} <br />12 January 2010<br />Knowx innovation<br />23<br />
  29. 29. getpid() - to get the current process ID.<br />getppid() - to get the parent process ID.<br />getuid() - to get the user ID.<br />getgid() - to get the group ID.<br />12 January 2010<br />Knowx innovation<br />24<br />
  30. 30. Program<br />#include &lt;stdio.h&gt; <br />#include &lt;unistd.h&gt; <br />#include &lt;sys/types.h&gt; <br />int main()<br />{<br />pid_t myPid; <br />pid_t myParentPid; <br />gid_t myGid; <br />uid_t myUid; <br />myPid = getpid(); <br />myParentPid = getppid(); <br />myGid = getgid();<br />12 January 2010<br />Knowx innovation<br />25<br />
  31. 31. myUid = getuid(); <br /> printf( &quot;my process id is %d &quot;, myPid ); <br /> printf( &quot;my parent&apos;s process id is %d &quot;, myParentPid );<br />printf( &quot;my group id is %d &quot;, myGid );<br />printf( &quot;my user id is %d &quot;, myUid ); return 0; <br />} <br />Output<br />my process id is 10932.<br />my parents process id is 10795<br />My group id is 500<br />My user id is 500<br />12 January 2010<br />Knowx innovation<br />26<br />
  32. 32. Process api<br />Fork<br />Create a new child process<br />Wait<br />Suspend execution until a child processes exits<br />Signal<br />Install a new signal handler<br />Exec<br />Replace the current process image with a new process image<br />12 January 2010<br />Knowx innovation<br />27<br />
  33. 33. Fork<br />Creating new processes with in a given process.<br />return value of fork is <br />greater than zero then in the parent context. <br />Equal to zero then in the child context.<br />less than zero then error occurred.<br />12 January 2010<br />Knowx innovation<br />28<br />
  34. 34. Example<br />pid_t pid;<br />pid = fork();<br />if(pid&gt;0)<br /> {/*parent context*/}<br />else if (pid ==0)<br /> {/*child context*/}<br />else<br />{/*error occurred no child created*/}<br />12 January 2010<br />Knowx innovation<br />29<br />
  35. 35. 12 January 2010<br />Knowx innovation<br />30<br />
  36. 36. Program<br />#include &lt;sys/types.h&gt; <br />#include &lt;sys/wait.h&gt; <br />#include &lt;unistd.h&gt;<br />#include &lt;stdio.h&gt; <br />#include &lt;errno.h&gt; <br />int main() <br />{<br />pid_t ret; <br />int status, I, role = -1;<br />12 January 2010<br />Knowx innovation<br />31<br />
  37. 37. ret = fork();<br /> if (ret &gt; 0)<br /> { printf(&quot;Parent: This is the parent process (pid %d) &quot;, getpid());<br /> for (i = 0 ; i &lt; 6 ; i++) <br /> {<br /> printf(&quot;Parent: At count %d &quot;, i); sleep(1); }<br /> ret = wait( &status ); <br /> role = 0; <br /> }<br />else if (ret == 0)<br /> {<br /> printf(&quot;Child: This is the child process (pid %d) &quot;, getpid()); <br />12 January 2010<br />Knowx innovation<br />32<br />
  38. 38. for (i = 0 ; i &lt; 6 ; i++) <br /> {<br />printf(&quot;Child: At count %d &quot;, i); <br /> sleep(1); } role = 1;<br /> }<br />else <br /> { <br /> printf(&quot;Parent: Error trying to fork() (%d) &quot;, errno); <br /> }<br />printf(&quot;%s: Exiting... &quot;, ((role == 0) ? &quot;Parent&quot; : &quot;Child&quot;)); return 0; <br />} <br />12 January 2010<br />Knowx innovation<br />33<br />
  39. 39. wait<br />Suspend the calling process until a child process(created by this process) exits or until a signal is delivered.<br />Prototype<br />pid_t wait (int *status);<br />12 January 2010<br />Knowx innovation<br />34<br />
  40. 40. Signal<br />Install a signal handler for a process.<br />Prototype<br />sighandler_t signal (int signum, sighandler_t handler);<br />SIGHUP - hang up –commonly used to restart a task.<br />SIGKILL - kill signal<br />SIGINT - interrupt from the keyboard<br />SIGSTOP – stop process<br />SIGQUIT – quit signal from keyboard<br />12 January 2010<br />Knowx innovation<br />35<br />
  41. 41. Program<br />#include &lt;stdio.h&gt; <br />#include &lt;sys/types.h&gt;<br />#include &lt;sys/wait.h&gt;<br />#include &lt;unistd.h&gt; <br />#include &lt;signal.h&gt;<br />#include &lt;errno.h&gt; <br />void usr1_handler( int sig_num ) <br />{<br /> printf( &quot;Process (%d) got the SIGUSR1 &quot;, getpid() );<br /> }<br />int main() <br />{ <br />pid_t ret;<br />int status role = -1; <br />signal( SIGUSR1, usr1_handler ); <br />12 January 2010<br />Knowx innovation<br />36<br />
  42. 42. ret = fork();<br />if (ret &gt; 0)<br /> { /* Parent Context */<br />printf( &quot;Parent: This is the parent process (pid %d) &quot;, getpid() ); <br />role = 0; <br />pause(); <br />printf( &quot;Parent: Awaiting child exit &quot; );<br />ret = wait( &status );<br />}<br />12 January 2010<br />Knowx innovation<br />37<br />
  43. 43. else if (ret == 0) <br />{ /* Child Context */ <br />printf( &quot;Child: This is the child process (pid %d) &quot;, getpid() );<br />role = 1; <br />pause(); <br />} <br />else <br />{ /* Parent Context -- Error */ <br />printf( &quot;Parent: Error trying to fork() (%d) &quot;, errno ); <br />}<br />printf( &quot;%s: Exiting... &quot;, ((role == 0) ? &quot;Parent&quot; : &quot;Child&quot;) ); return 0;<br />} <br />12 January 2010<br />Knowx innovation<br />38<br />
  44. 44. exec<br />Replaces the current process image altogether.<br />Once the exec function replaces the current process, its pid is the same as the creating process<br />Permits the current process context to be replace with the program specifies as the first argument.<br />12 January 2010<br />Knowx innovation<br />39<br />
  45. 45. Example<br /> execl(“/bin/ls”,”ls”,”-la”,null);<br />This command replaces the current process with the ls (list directory)<br />Prototypes for the variants of exec<br />int execl (const char *path,const char * arg, …,..)<br />int execlp(const char *path,const char *arg,……)<br />int execv(const char *path, char *const argv[])<br />int execvp(const char *file, char *const argv[])<br />12 January 2010<br />Knowx innovation<br />40<br />
  46. 46. Program for shell interpreter<br />#include &lt;sys/types.h&gt; <br />#include &lt;sys/wait.h&gt;<br />#include &lt;unistd.h&gt;<br />#include &lt;stdio.h&gt;<br />#include &lt;stdlib.h&gt; <br />#include &lt;string.h&gt; <br />#define MAX_LINE 80 <br />int main() <br />{<br />int status; <br />pid_t childpid;<br />char cmd[MAX_LINE+1]; <br />char *sret;<br />12 January 2010<br />Knowx innovation<br />41<br />
  47. 47. while (1) <br />{<br />printf(&quot;mysh&gt;&quot;); <br />sret = fgets( cmd, sizeof(cmd), stdin ); <br />if (sret == NULL) <br />exit(-1);<br />cmd[ strlen(cmd)-1] = 0; <br />if (!strncmp(cmd, &quot;bye&quot;, 3)) <br />exit(0);<br />childpid = fork(); <br />if (childpid == 0)<br /> { execlp( cmd, cmd, 0 ); }<br />else if (childpid &gt; 0) <br /> { waitpid( childpid, &status, 0 ); } <br />printf(&quot; &quot;); <br />} <br />return 0;<br />} <br />12 January 2010<br />Knowx innovation<br />42<br />
  48. 48. Message queues<br />Messages are small collections of data (400 bytes, for example) that can be passed between cooperating programs through a message queue.<br />Messages within a queue can be of different types, and any process with proper permissions can receive the messages.<br />12 January 2010<br />Knowx innovation<br />43<br />
  49. 49. <ul><li>Creating message queue </li></ul>API<br />msgget(key_k key , int msgflg)<br />the key which signifies the name given to the queue<br />flags argument must contain the permission bits for the new queue<br />IPC_CREAT if the queue is being created<br />IPC_EXCL return an error if the message queue already exists<br />Return the msgid<br />Example <br />msgget(111 , 0666 | IPC_CREAT);<br />12 January 2010<br />Knowx innovation<br />44<br />
  50. 50. Program<br />#include &lt;stdio.h&gt; <br />#include &lt;sys/msg.h&gt;<br />#define MY_MQ_ID 111<br />int main()<br />{<br />int msgid;<br />/* Create the message queue with the id MY_MQ_ID */ <br />msgid = msgget( MY_MQ_ID, 0666 | IPC_CREAT ); <br />if (msgid &gt;= 0) <br /> { <br />printf( &quot;Created a Message Queue %d &quot;, msgid ); <br />}<br />return 0;<br />} <br />12 January 2010<br />Knowx innovation<br />45<br />
  51. 51. <ul><li>Sending the message</li></ul>API<br />int msgsnd (int msqid, const void *msgp, size_t msgsz, int msgflg); <br />Takes 4 parameters<br />msqid is the queue id of the existing queue.<br />msgp is the pointer that contains the address of the structure that holds the message and type.<br />struct message<br />{ long mtype; //The message type.priority<br />char mesg [MSGSZ];//The message is of length MSGSZ. }; <br />12 January 2010<br />Knowx innovation<br />46<br />
  52. 52. MSGSZ is the length of the message sent in bytes.<br />MSGFLG specifies the action to be taken if one or more of the following are true.<br />The number of bytes already in the queue is equal to msg_qbytes.<br />The total number of messages on all queues on the system has reached a maximum limit<br />Action to be taken<br />If (msgflg & IPC_NOWAIT) is non-zero, the message will not be sent and the calling process will return immediately<br />If (msgflg & IPC_NOWAIT) is 0, the calling process will suspend execution until one of the following occurs: <br />12 January 2010<br />Knowx innovation<br />47<br />
  53. 53. Program<br />#include &lt;sys/types.h&gt; <br />#include &lt;sys/ipc.h&gt; <br />#include &lt;sys/msg.h&gt;<br />#include &lt;stdio.h&gt; <br />#include &lt;string.h&gt; <br />#define MSGSZ 128 <br />typedef struct msgbuf /* will hold the message to be put in the queue */<br />{ long mtype; /* priority of message */<br />char mtext[MSGSZ]; /* the message that is stored */<br /> }<br />message_buf; <br />main() <br />{ int msqid; <br />int msgflg = IPC_CREAT | 0666; <br />key_t key; message_buf sbuf; size_t buf_length; key = 10;<br />printf(&quot;Calling msgget with key %#lx and flag %#o &quot;,key,msgflg);<br />12 January 2010<br />Knowx innovation<br />48<br />
  54. 54. /*A queue is created using the msget function with a key value 10 and the flag parameter being IPC_CREAT|06668 */<br /> if ((msqid = msgget(key, msgflg )) &lt; 0)<br />{ perror(&quot;msgget&quot;); <br /> exit(1); } <br />else <br />printf(&quot;msgget: msgget succeeded: msqid = %d &quot;, msqid); <br />sbuf.mtype = 1; /*setting the priority as 1 */<br />printf(&quot;msgget: msgget succeeded: msqid = %d &quot;, msqid);<br />(void) strcpy(sbuf.mtext, &quot;I am in the queue?&quot;); <br />/* copy the text &quot;I am in the queue&quot; into the array mtext which is message array */<br />printf(&quot;msgget: msgget succeeded: msqid = %d &quot;, msqid);<br />12 January 2010<br />Knowx innovation<br />49<br />
  55. 55. buf_length = strlen(sbuf.mtext) + 1 ; <br />/*sending the message with option IPC_NOWAIT */<br />if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) &lt; 0)<br />{ <br />printf (&quot;%d, %d, %s, %d &quot;, msqid, sbuf.mtype, sbuf.mtext, buf_length); <br /> perror(&quot;msgsnd&quot;); <br /> exit(1); <br />} <br />else printf(&quot;Message: &quot;%s&quot; Sent &quot;, sbuf.mtext); <br />exit(0);<br /> }<br />12 January 2010<br />Knowx innovation<br />50<br />
  56. 56. <ul><li>Receive the message</li></ul>API<br />int msgrcv (int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);<br />Arguments-<br />msqid is the queue id of the existing queue.<br />msgp is the point to a receiving buffer large enough to hold the received message<br />msgszthe maximum size of the received message<br />12 January 2010<br />Knowx innovation<br />51<br />
  57. 57. program<br />#include &lt;sys/types.h&gt; <br />#include &lt;sys/ipc.h&gt; <br />#include &lt;sys/msg.h&gt; <br />#include &lt;stdio.h&gt; <br />#define MSGSZ 128 <br />typedef struct msgbuf /* structure that will hold the message obtained from the queue*/<br />{<br /> long mtype; /*priority of message */<br /> char mtext[MSGSZ]; /*message that stored */<br />} message_buf;<br />main() <br />{<br />12 January 2010<br />Knowx innovation<br />52<br />
  58. 58. int msqid; <br />key_t key; <br />message_buf rbuf; <br />key = 10;<br />/*creating a queue with the key value 10, */<br />if ((msqid = msgget(key, 0666)) &lt; 0)<br />{ perror(&quot;msgget&quot;); <br />exit(1); } <br />/*acquires the message from the queue into rbuf*/<br />if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) &lt; 0) <br /> { perror(&quot;msgrcv&quot;);<br />exit(1); }<br />printf(&quot;%s &quot;, rbuf.mtext); <br />exit(0); <br />} <br />12 January 2010<br />Knowx innovation<br />53<br />
  59. 59. <ul><li>Configuring message queue</li></ul>API<br />msgctl(int msqid, int cmd, struct msqid_ds *buf)<br />3 Arguments <br />msqid is the queue id <br />cmd is a command constant<br />*bufis a pointer to structure.<br />Use to get the info about a message queue<br />Set the info for a message queue<br />Remove a message queue<br />12 January 2010<br />Knowx innovation<br />54<br />
  60. 60. 12 January 2010<br />Knowx innovation<br />55<br />
  61. 61. Variable that is protected.<br />It provides a means to restrict access to a resource that is shared amongst two or more processes<br />Two operations are permitted , called acquire and release.<br />2 types of semaphore.<br />Binary and Counting <br />12 January 2010<br />Knowx innovation<br />56<br />Semaphore<br />
  62. 62. The signal notations<br />P(semaphore variable) for wait<br />V(semaphore variable) for signal<br />12 January 2010<br />Knowx innovation<br />57<br />
  63. 63. A binary semaphore is a variable that can take only the values 0 and 1.<br />Represents a single resource and therefore when one process has acquired it,others are blocked until it is released.<br />P(sv) if sv greater than 0 , decrement sv.ifsvis zero suspend execution of the process<br />V(sv) if other process has been suspended waiting for sv, make it resume.if no waiting, increment sv<br />12 January 2010<br />Knowx innovation<br />58<br />Binary semaphore<br />
  64. 64. 12 January 2010<br />Knowx innovation<br />59<br />
  65. 65. Represent shared resources in quantities greater than one.<br />It could represent the entire set of buffers by setting its value to the number of buffers available.<br />Whenever the process acquires the semaphore, the value decrement .<br />When the semaphore value reaches zero, process are blocked until it becomes a non zero.<br />12 January 2010<br />Knowx innovation<br />60<br />Counting semaphore<br />
  66. 66. 12 January 2010<br />Knowx innovation<br />61<br />
  67. 67. <ul><li>Creating semaphore</li></ul>API<br />semget( key_t key, int num_sems, int sem_flags)<br />Key is the semaphore key<br />Num_sems is the Semaphore count<br />Flags <br /><ul><li>To acquire or release of semaphore</li></ul>API<br />semop(int sem_id, struct sembuf *sem_ops,size_tnum_sem_ops)<br />12 January 2010<br />Knowx innovation<br />62<br />
  68. 68. sem_id, is the semaphore identifier, as returned from semget.<br />sem_ops, is a pointer to an array of structures, each of which will have at least the following members:<br />Struct sembuf<br />{short sem_num;<br />short sem_op;<br />short sem_flg;<br />}<br />12 January 2010<br />Knowx innovation<br />63<br />
  69. 69. Program to acquire the semaphore.<br />#define MY_SEM_ID 111 <br />#define MY_SEMARRAY_ID 112 <br />#define NUM_SEMAPHORES 10 <br />#include &lt;stdio.h&gt; <br />#include &lt;sys/sem.h&gt; <br />#include &lt;stdlib.h&gt;<br />#include &quot;common.h&quot; <br />int main() <br />{ <br />int semid; <br />struct sembufsb;<br />/* Get the semaphore with the id MY_SEM_ID */<br />semid = semget( MY_SEM_ID, 1, 0 );<br />12 January 2010<br />Knowx innovation<br />64<br />
  70. 70. if (semid &gt;= 0) <br />{<br />sb.sem_num = 0; <br />sb.sem_op= -1; <br />sb.sem_flg= 0;<br />printf( &quot;semacq: Attempting to acquire semaphore %d &quot;, semid ); <br />/* Acquire the semaphore */ <br /> if (semop( semid, &sb, 1 ) == -1) <br /> {<br />printf(&quot;semacq: semop failed. &quot;); <br />exit(-1);<br />} <br /> printf( &quot;semacq: Semaphore acquired %d &quot;, semid);<br /> }<br />return 0;<br />} <br />12 January 2010<br />Knowx innovation<br />65<br />
  71. 71. Program to release the semaphore.<br />#define MY_SEM_ID 111 <br />#define MY_SEMARRAY_ID 112 <br />#define NUM_SEMAPHORES 10 <br />#include &lt;stdio.h&gt; <br />#include &lt;sys/sem.h&gt; <br />#include &lt;stdlib.h&gt;<br /> #include &quot;common.h&quot; <br />int main() <br />{ <br />int semid; <br />struct sembufsb;<br /> /* Get the semaphore with the id MY_SEM_ID */<br />semid = semget( MY_SEM_ID, 1, 0 );<br />12 January 2010<br />Knowx innovation<br />66<br />
  72. 72. if (semid &gt;= 0) <br />{<br />printf( &quot;semrel: Releasing semaphore %d &quot;, semid);<br />sb.sem_num = 0; <br />sb.sem_op= 1; <br />sb.sem_flg= 0; <br />/* Release the semaphore */ <br />if (semop( semid, &sb, 1 ) == -1) <br />{<br /> printf(&quot;semrel: semop failed. &quot;); <br />exit(-1); <br />}<br />printf( &quot;semrel: Semaphore released %d &quot;, semid ); <br />}<br />return 0;<br />} <br />12 January 2010<br />Knowx innovation<br />67<br />
  73. 73. 12 January 2010<br />Knowx innovation<br />68<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×