Академический Документы
Профессиональный Документы
Культура Документы
Operating System
Lab
Submitted To:
Prof. SK Dhurandher
Submitted By:
Gaurav Jain
733/IT/14
Index
S. NO. Topic Date Signature
int main()
{
int fd1;
int main()
{
int fd;
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 128
struct msgbuffer
{
long mtype;
char mtext[MAXSIZE];
};
main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
msgbuffer sbuf;
size_t buflen;
key = 1234;
sbuf.mtype = 1;
buflen = strlen(sbuf.mtext) + 1 ;
else
printf("Message Sent\n");
exit(0);
}
B. Message Receive
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 128
struct msgbuffer
{
long mtype;
char mtext[MAXSIZE];
};
main()
{
int msqid;
key_t key;
msgbuffer rcvbuffer;
key = 1234;
printf("%s\n", rcvbuffer.mtext);
exit(0);
}
Output:
Message send Message receive
3. Unidirectional and Bidirectional pipes
Unidirectional pipe
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MSG_LEN 64
int main(){
int result;
int fd[2];
char *message="Linux World!!!\n";
char recvd_msg[MSG_LEN];
result = pipe (fd); //Creating a pipe fd[0] is for reading and fd[1] is for writing
if (result < 0) {
perror("pipe ");
exit(1);
}
result=write(fd[1],message,strlen(message));
if (result < 0) {
perror("write");
exit(2);
}
result=read (fd[0],recvd_msg,MSG_LEN);
if (result < 0) {
perror("read");
exit(3);
}
printf("%s\n",recvd_msg);
return 0;
}
Output:
Bidirectional pipes
#include<stdio.h>
#include<fcntl.h>
main(){
int pfd1[2],pfd2[2],p,x1,x2;
char msg1[]="From parent: Yo Man\n";
char msg2[]="From child: How are you?\n"; char buf[20];
x1=strlen(msg1);
x2=strlen(msg2);
pipe(pfd1);
pipe(pfd2);
p=fork();
if(p==0){//from parent to child
close(pfd1[1]);
read(pfd1[0],buf,x1); printf("\nchild recieved:%s",buf);
}
else{
close(pfd1[0]);
write(pfd1[1],msg1,x1);
}
if(p==0){//from child to parent
close(pfd2[0]);
write(pfd2[1],msg2,x2);
}
else{
close(pfd2[1]);
read(pfd2[0],buf,x2); printf("\nparent recieved:%s",buf);
}
return 0;
}
Output:
4. Socket Programming
Socket server
// Server side C/C++ program to demonstrate Socket programming
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server Gaurav";
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
#define SHMSZ 27
main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
key = 5678;
printf("Shared memory segment was successfully generated with Shared Memory Id : %d\n\n",
(int)shmid);
s = shm;
exit(0);
}
Second User
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SHMSZ 27
main()
{
int shmid;
key_t key;
char *shm, *s;
key = 5678;
printf("Requesting access to the shared memory segment with key : %d ....\n\n", key);
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
perror("shmget");
exit(1);
}
printf("Shared memory segment was successfully accessed with Shared Memory Id : %d\n\n",
(int)shmid);
printf("Reading data...\n\n");
*shm = '*';
exit(0);
}
Output:
First User
Second User