Anda di halaman 1dari 54

UNIX INTERNALS LAB

1.AIM:
Write a program to create pipes?
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#define MAX 50
main()
{
pid_t pid;
int fd[2];
char line[MAX];
if (pipe(fd)<0)
{
perror("PIPE");
exit(1);
}
pid=fork();
if (pid<0)
{
perror("FORK");
exit(1);
}

if (pid==0)

AVR & SVR CET

Page 1

UNIX INTERNALS LAB


{
printf("CHILD IS ACTIVE...\n");
close(fd[1]);
read(fd[0],line,MAX);
printf("Parent Message is ...%s\n",line);
}
else
{
printf("PARENT IS ACTIVE...\n");
close(fd[0]);
write(fd[1],"How are You",11);
}
}

EXECUTION STEPS:
[sampath@localhost ipc]$. vi pipes.c
[sampath@localhost ipc]$. cc pipes.c
[sampath@localhost ipc]$. ./a.out

PARENT IS ACTIVE
CHILD IS ACTIVE...

AVR & SVR CET

Page 2

UNIX INTERNALS LAB


Parent Message is ... How are You

2.AIM:
Write a program for FIFO?
PROGRAM

AVR & SVR CET

Page 3

UNIX INTERNALS LAB


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_NAME "SUHRIT_SOLUTIONS"
#define FIFO
int main(void)
{
char s[300];
int num, fd;
mknod(FIFO_NAME, S-I FIFO | 0666, 0);
printf("Waiting for writers...\n");
fd = open(FIFO_NAME, O_RDONLY);
printf("Yes! got a writer\n");

do
{
if ((num = read(fd, s, 300)) == -1)
perror("read");
else
{

AVR & SVR CET

Page 4

UNIX INTERNALS LAB


s[num] = '\0';
printf("Solutions: read %d bytes: \"%s\"\n",
num, s);
}
} while (num > 0);

return 0;
}

EXECUTION STEPS:
[sampath@localhost ipc]$. vi fifo.c
[sampath@localhost ipc]$. cc fifo.c
[sampath@localhost ipc]$. ./a.out

Waiting for writers...


Yes! got a writer
abcd
Solutions: read 4 bytes:abcd.

AVR & SVR CET

Page 5

UNIX INTERNALS LAB

3.AIM:
Write a program to create, write and read from shared memory?
PROGRAM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

AVR & SVR CET

Page 6

UNIX INTERNALS LAB


int main(void)
{
pid_t pid;
int *shared; /* pointer to the shm */
int shmid;
shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
printf("Shared Memory ID=%u",shmid);
if (fork() == 0)
{
/* Child */ /* Attach to shared memory and print the pointer */
shared = shmat(shmid, (void *) 0, 0);
printf("Child pointer %u\n", shared);
*shared=1;
printf("Child value=%d\n", *shared);
sleep(2);
printf("Child value=%d\n", *shared);
}

else
{
/* Parent */ /* Attach to shared memory and print the pointer */
shared = shmat(shmid, (void *) 0, 0);

AVR & SVR CET

Page 7

UNIX INTERNALS LAB


printf("Parent pointer %u\n", shared);
printf("Parent value=%d\n", *shared);
sleep(1);
*shared=42;
printf("Parent value=%d\n", *shared);
sleep(5);
shmctl(shmid, IPC_RMID, 0);
}
}

EXECUTION STEPS:

AVR & SVR CET

Page 8

UNIX INTERNALS LAB


[sampath@localhost ipc]$vi shared_mem.c
[sampath@localhost ipc]$cc shared_mem.c
[sampath@localhost ipc]$ ./a.out
Shared Memory ID=65537Child pointer 3086680064
Child value=1
Shared Memory ID=65537Parent pointer 3086680064
Parent value=1
Parent value=42
Child value=42

AVR & SVR CET

Page 9

UNIX INTERNALS LAB

4.AIM:
Write a program to implement various operations on Message Queue ?
PROGRAM
message_send.c -- creating and sending to a simple message queue
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#define MSGSZ

128

/* Declare the message structure. */


typedef struct msgbuf
{
long

mtype;

char

mtext[MSGSZ];

} message_buf;

main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
message_buf sbuf;

AVR & SVR CET

Page 10

UNIX INTERNALS LAB


size_t buf_length;
/* Get the message queue id for the "name" 1234, which was created by the
server. */
key = 1234;
(void) fprintf(stderr, "\nmsgget: Calling msgget(%#lx,\ %#o)\n", key, msgflg);

if ((msqid = msgget(key, msgflg )) < 0)


{
perror("msgget");
exit(1);
}
else
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
/* We'll send message type */
sbuf.mtype = 1;
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
(void) strcpy(sbuf.mtext, "Did you get this?");
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
buf_length = strlen(sbuf.mtext) + 1 ;
/* Send a message.*/
if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0)
{
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
perror("msgsnd");
exit(1);
AVR & SVR CET

Page 11

UNIX INTERNALS LAB


}
else
printf("Message: \"%s\" Sent\n", sbuf.mtext);
exit(0);
}

message_rec.c -- receiving the above message


#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#define MSGSZ

128

/* Declare the message structure. */


typedef struct msgbuf
{
long

mtype;

char

mtext[MSGSZ];

} message_buf;
main()
{
int msqid;
key_t key;

AVR & SVR CET

Page 12

UNIX INTERNALS LAB


message_buf rbuf;
/* Get the message queue id for the "name" 1234, which was created by the server */
key = 1234;
if ((msqid = msgget(key, 0666)) < 0)
{
perror("msgget");
exit(1);
}
if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0)
{
perror("msgrcv");
exit(1);
}
/* Print the answer. */
printf("%s\n", rbuf.mtext);
exit(0);
}

AVR & SVR CET

Page 13

UNIX INTERNALS LAB

EXECUTION STEPS:
[sampath@localhost msgque]vi message_send.c
[sampath@localhost msgque]cc message_send.c
[sampath@localhost msgque]mv a.out msgsend
[sampath@localhost msgque]$ ./msgsend
msgget: Calling msgget(0x4d2 , 01666)
msgget: msgget succeeded: msqid = 0
msgget: msgget succeeded: msqid = 0
msgget: msgget succeeded: msqid = 0
Message: "Did you get this?" Sent
AVR & SVR CET

Page 14

UNIX INTERNALS LAB

[sampath@localhost msgque]vi message_rec.c


[sampath@localhost msgque]cc message_rec.c
[sampath@localhost msgque]mv a.out msgrec
[sampath@localhost msgque]$ ./msgrec &
[1] 2907
[sampath@localhost msgque]$ Did you get this?

5.AIM:
Perform socket programming using UDP?
PROGRAM
udp_server.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
AVR & SVR CET

Page 15

UNIX INTERNALS LAB


#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#define BUFSIZE 512
#define MYPORT 11710
#define MAXNAME 100
int main(int C, char **V )
{
int sd,n,ret;
struct sockaddr_in serveraddress,cliaddr;
socklen_t length;
char clientname[MAXNAME],datareceived[BUFSIZE];
sd = socket( AF_INET, SOCK_DGRAM, 0 );
if( sd < 0 )
{
perror( "socket" );
exit( 1 );
}
memset( &serveraddress, 0, sizeof(serveraddress) );
memset( &cliaddr, 0, sizeof(cliaddr) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(MYPORT);//PORT NO
serveraddress.sin_addr.s_addr = htonl(INADDR_ANY);//IP ADDRESS

AVR & SVR CET

Page 16

UNIX INTERNALS LAB


ret=bind(sd,(struct sockaddr*)&serveraddress,sizeof(serveraddress));
if(ret<0)
{
perror("BIND FAILS");
exit(1);
}

for(;;)
{
printf("I am waiting\n");
/*Received a datagram*/
length=sizeof(cliaddr);
n=recvfrom(sd,datareceived,BUFSIZE,0, (struct sockaddr*)&cliaddr ,
&length);
printf("Data Received from %s\n", net_ntop(AF_INET,&cliaddr.sin_addr,
clientname,sizeof(clientname)));
/*Sending the Received datagram back*/
datareceived[n]='\0';
printf("I have received %s\n",datareceived);
sendto(sd,datareceived,n,0,(struct sockaddr *)&cliaddr,length);
}
}

udp_client.c
#include <stdio.h>
#include <stdlib.h>
AVR & SVR CET

Page 17

UNIX INTERNALS LAB


#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#define BUFSIZE 512
static void sig_usr(int);
void str_cli(FILE *fp , int sockfd , struct sockaddr *server , socklen_t len);
int main( int C, char *argv[] )
{
int sd;
struct sockaddr_in serveraddress;
/*Installing signal Handlers*/
signal(SIGPIPE,sig_usr);
signal(SIGINT,sig_usr);
if (NULL==argv[1])
{
printf("Please enter the IP Address of the server\n");
exit(0);
}

AVR & SVR CET

Page 18

UNIX INTERNALS LAB

if (NULL==argv[2])
{
printf("Please enter the Port Number of the server\n");
exit(0);
}
sd = socket( AF_INET, SOCK_DGRAM, 0 );
if( sd < 0 )
{
perror( "socket" );
exit( 1 );
}

memset( &serveraddress, 0, sizeof(serveraddress) );


serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(atoi(argv[2]));//PORT NO
serveraddress.sin_addr.s_addr = inet_addr(argv[1]);//ADDRESS
printf("Client Starting service\n");
printf("Enter Data For the server\n");
str_cli(stdin,sd ,(struct sockaddr *)&serveraddress, sizeof(serveraddress));
}
static void sig_usr( int signo) /*Signal Number*/
{
char *strpipe="RECEIVED SIGPIPE - ERROR";
char *strctrl="RECEIVED CTRL-C FROM YOU";

AVR & SVR CET

Page 19

UNIX INTERNALS LAB


if(signo==SIGPIPE)
{
write(1,strpipe,strlen(strpipe));
exit(1);
}
else if(signo==SIGINT)
{
write(1,strctrl,strlen(strctrl));
exit(1);
}
}
void str_cli(FILE *fp,

/*Here to be used as stdin as argument*/

int sockfd ,
struct sockaddr *to ,socklen_t length) /*Connection Socket */
{
int maxdes,n;
fd_set rset;
char sendbuf[BUFSIZE] , recvbuf[BUFSIZE] ,servername[100];
struct sockaddr_in serveraddr;
socklen_t slen;
FD_ZERO(&rset);
maxdes=(sockfd>fileno(fp)?sockfd+1:fileno(fp)+1);
for(;;)
{
FD_SET(fileno(fp) , &rset);

AVR & SVR CET

Page 20

UNIX INTERNALS LAB


FD_SET(sockfd , &rset);
select(maxdes,&rset,NULL,NULL,NULL);
if(FD_ISSET(sockfd , & rset))
{
slen=sizeof(serveraddr);
n=recvfrom(sockfd,recvbuf,BUFSIZE,0, (struct
sockaddr*)&serveraddr,&slen);
printf("Data Received from server %s:\n",
inet_ntop(AF_INET,&serveraddr.sin_addr,
servername,sizeof(servername)));
write(1,recvbuf,n);
printf("Enter Data For the server\n");
}
if(FD_ISSET(fileno(fp) , & rset))
{
/*Reading data from the keyboard*/
fgets(sendbuf,BUFSIZE,fp);
n = strlen (sendbuf);
/*Sending the read data over socket*/
sendto(sockfd,sendbuf,n,0,to,length);
printf("Data Sent To Server\n");
}
}
}

AVR & SVR CET

Page 21

UNIX INTERNALS LAB

EXECUTION STEPS:
UDP Client Server Application.
Compiling and running server.
[user@localhost week9]$ vi udp_server.c
[user@localhost week9]$ cc udp_server.c
[user@localhost week9]$ mv a.out udp_server
[user@localhost week9]$ ./ udp_server
I am waiting
Data Received from 127.0.0.1
I have received abcd efgh
rev is
hgfe dcba
I am waiting
Compiling and running client.
user@localhost week9]$ vi udp_client.c
AVR & SVR CET

Page 22

UNIX INTERNALS LAB


user@localhost week9]$ cc udp_client.c
[user@localhost week9]$ mv a.out udp_client
[user@localhost week9]$ ./ udp_client 127.0.0.1 11710
Client Starting service
Enter Data For the server
abcd efgh
Data Sent To Server
Data Received from server 127.0.0.1:
abcd efgh

6.AIM:
Perform socket programming using TCP ?
PROGRAM

tcpservselect01.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
AVR & SVR CET

Page 23

UNIX INTERNALS LAB


#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MAXLINE 100
#define SERV_PORT 13153
int main(int argc, char **argv)
{
int

k, i, maxi, maxfd, listenfd, connfd, sockfd;

int

nready, client[FD_SETSIZE];

ssize_t

n;

fd_set

rset, allset;

char

line[MAXLINE],buf[100];

socklen_t

clilen;

struct sockaddr_in cliaddr, servaddr;


listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0 )
{
perror("socket" );
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family

= AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

AVR & SVR CET

Page 24

UNIX INTERNALS LAB


servaddr.sin_port

= htons(SERV_PORT);

bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));


listen(listenfd,5);
maxfd = listenfd;
maxi = -1;

/* initialize */

/* index into client[] array */

for (i = 0; i < FD_SETSIZE; i++)


client[i] = -1;

/* -1 indicates available entry */

FD_ZERO(&allset);
FD_SET(listenfd, &allset);
/* end fig01 */ /* include fig02 */
for ( ; ; )
{
printf("Server:I am waiting-----Start of Main Loop\n");
rset = allset;

/* structure assignment */

nready = select(maxfd+1, &rset, NULL, NULL, NULL);


if (FD_ISSET(listenfd, &rset)) { /* new client connection */
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
#ifdef NOTDEF
printf("new client: %s, port %d\n", inet_ntop(AF_INET, cliaddr.sin_addr,
buf, NULL), ntohs(cliaddr.sin_port));
#endif
for (i = 0; i < FD_SETSIZE; i++)
if (client[i] < 0)
{
client[i] = connfd; /* save descriptor */
AVR & SVR CET

Page 25

UNIX INTERNALS LAB


break;
}
if (i == FD_SETSIZE)
{
printf("too many clients");
exit(0);
}
FD_SET(connfd, &allset); /* add new descriptor to set */
if (connfd > maxfd)
maxfd = connfd;

/* for select */

if (i > maxi)
maxi = i;

/* max index in client[] array */

if (--nready <= 0)
continue;

/* no more readable descriptors */

}
for (i = 0; i <= maxi; i++)
{
/* check all clients for data */
if ( (sockfd = client[i]) < 0)
continue;
if (FD_ISSET(sockfd, &rset))
{
if ( (n = read(sockfd, line, MAXLINE)) == 0)
{
/*4connection closed by client */

AVR & SVR CET

Page 26

UNIX INTERNALS LAB


close(sockfd);
FD_CLR(sockfd, &allset);
client[i] = -1;
}
else
{
printf("\n output at server\n");
for(k=0;line[k]!='\0';k++)
printf("%c",toupper(line[k]));
write(sockfd, line, n);
}
if (--nready <= 0)
break;

/* no more readable descriptors */

}
}

AVR & SVR CET

Page 27

UNIX INTERNALS LAB


tcpclient.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MAXBUFFER 1024
void sendstring(int , char *);
int main( int C, char *V[] )
{
int sd,fd;
char c;
struct sockaddr_in serveraddress;
char text[100];
int i=0;
sd = socket( AF_INET, SOCK_STREAM, 0 );
if( sd < 0 )
{
perror( "socket" );

AVR & SVR CET

Page 28

UNIX INTERNALS LAB


exit( 1 );
}

if (V[1] == NULL )
{
printf ("PL specfiy the server's IP Address \n");
exit(0);
}
if (V[2] == NULL )
{
printf ("PL specify the server's Port No \n");
exit(0);
}
memset( &serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(atoi(V[2]));//PORT NO
serveraddress.sin_addr.s_addr = inet_addr(V[1]);//ADDRESS
if (connect(sd,(struct sockaddr*)&serveraddress, sizeof(serveraddress))<0)
{
printf("Cannot Connect to server");
exit(1);
}
printf("enter sentence to end enter #");
while(1)
{

AVR & SVR CET

Page 29

UNIX INTERNALS LAB


c=getchar();
if(c=='#')
break;
text[i++]=c;
}

text[i]='\0';
sendstring(sd,text);
close(sd);
return 0;
}
void sendstring ( int sd, char *fname) /*Socket Descriptor*/ /*Array Containing the
string */
{
int n , byteswritten=0 , written ;
char buffer[MAXBUFFER];
strcpy(buffer , fname);
n=strlen(buffer);
while (byteswritten<n)
{
written=write(sd , buffer+byteswritten,(n-byteswritten));
byteswritten+=written;
}
printf("String : %s sent to server \n",buffer);
}

AVR & SVR CET

Page 30

UNIX INTERNALS LAB

EXECUTION STEPS:
b) Concurrent Server Application Using Select.
Compiling and running server.
root@localhost week7and8]# vi tcpservselect01.c
root@localhost week7and8]# cc tcpservselect01.c
[root@localhost week7and8]# mv a.out tcpservselect1
[root@localhost week7and8]# ./tcpservselect1
Server:I am waiting-----Start of Main Loop
Server:I am waiting-----Start of Main Loop

output at server
abcd

Server:I am waiting-----Start of Main Loop

Compiling and running Client.

AVR & SVR CET

Page 31

UNIX INTERNALS LAB


root@localhost week7and8]# vi tcpclient.c
root@localhost week7and8]# cc tcpclient.c
[root@localhost week7and8]# mv a.out tcpclient

root@localhost week7and8]# ./tcpclient 127.0.0.1 13153


enter sentence to end enter #abcd#
String : abcd sent to server

7.AIM:
Write a program for process creation and process execution for display environment
variables.

PROGRAM

#include<stdio.h>
int main(int argc, char *argv[],char *env[])
{
int i;
printf(" my os environment: \n");
for(i=0;env[i];i++)
printf("%s\n", env[i]);
return 0;
AVR & SVR CET

Page 32

UNIX INTERNALS LAB


}

#include<stdio.h>
extern char ** environ;
int main()
{
int i;
printf(" my os environment\n");
for(i=0;environ[i];i++)
printf("%s\n",environ[i]);
return 0;
}

AVR & SVR CET

Page 33

UNIX INTERNALS LAB

EXECUTION STEPS:
[sampath@localhost ipc]$. vi env1.c
[sampath@localhost ipc]$. cc env1.c
[sampath@localhost ipc]$. ./a.out

my os environment

[sampath@localhost ipc]$. vi env2.c


[sampath@localhost ipc]$. cc env2.c
[sampath@localhost ipc]$. ./a.out

my os environment

AVR & SVR CET

Page 34

UNIX INTERNALS LAB

8. AIM:

Write a program on process creation and execution for implement different types of execution
function

PROGRAM

AVR & SVR CET

Page 35

UNIX INTERNALS LAB


#include<stdio.h>
#include<unistd.h>
int main()
{
char *const arg[] ={"one","two",(char*)0};
execv("./argv",arg);
printf(" display read line variables\n");
return 0;
}

#include<unistd.h>
int main()
{
execl("./hello","1",(char *)0);
printf(" display read line variables\n");
}

AVR & SVR CET

Page 36

UNIX INTERNALS LAB

EXECUTION STEPS:
[sampath@localhost ipc]$. vi env1.c
[sampath@localhost ipc]$. cc env1.c
[sampath@localhost ipc]$. ./a.out

displays read line variables

[sampath@localhost ipc]$. vi env2.c


[sampath@localhost ipc]$. cc env2.c

AVR & SVR CET

Page 37

UNIX INTERNALS LAB


[sampath@localhost ipc]$. ./a.out

displays read line variables

AVR & SVR CET

Page 38

UNIX INTERNALS LAB

9. AIM:
Write a program to create file and modify and to delete record

PROGRAM

#include<stdio.h>
int main(int argc, char *argv[])
{
char ch;
FILE *fp;
fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("file does not exit.\n");
return 0;

AVR & SVR CET

Page 39

UNIX INTERNALS LAB


}
while(!feof(fp))
{
ch=fgetc(fp);
printf("%c",ch);
}
fclose(fp);
return 0;
}

AVR & SVR CET

Page 40

UNIX INTERNALS LAB

EXECUTION STEPS:
[sampath@localhost ipc]$. vi file.c
[sampath@localhost ipc]$. cc file.c
[sampath@localhost ipc]$. ./a.out

file does not exist.

AVR & SVR CET

Page 41

UNIX INTERNALS LAB

AVR & SVR CET

Page 42

UNIX INTERNALS LAB

10.AIM:
Write a program to find status and mode value of a file

PROGRAM

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
char data[1];
char str[1024];
int main(int argc,char *argv[])
{
int count;
int ch;
int fd;
int pos,len;
if(argc!=2)
{
printf("command requires two arguments./n");
exit(1);
}
fd=open(argv[1],O_RDWR);

AVR & SVR CET

Page 43

UNIX INTERNALS LAB


if(fd<0)
{
printf("file does not exit./n");
exit(2);
}
do
{
printf("\n\t.........File Operations....\n");
printf("\tRead......1\n");
printf("\tWrite.....2\n");
printf("\tExit......3\n");
printf("Enter your choice(1-3):");
scanf("%d",&ch);
switch(ch)
{
case 1 :
printf("Enter file position in byte:");
scanf("%d",&pos);
printf("Enter number of byte to read:");
scanf("%d",&len);
lseek(fd,pos-1,SEEK_SET);
count=0;

AVR & SVR CET

Page 44

UNIX INTERNALS LAB

while(read(fd,data,1)>0)
{

count++;
write(1,data,1);
if(count==len)break;
}
break;
case 2 :
printf("Enter the position to write:");
scanf("%d",&pos);
printf("Enter the string towrite into to file:");
scanf("%s",str);
lseek(fd,pos-1,SEEK_SET);
write(fd,str,strlen(str));
break;
case 3 :
exit(0);
default:
printf("Invalid choice. Try again./n");
}
}while(1);
exit(0);
}

AVR & SVR CET

Page 45

UNIX INTERNALS LAB

AVR & SVR CET

Page 46

UNIX INTERNALS LAB


EXECUTION STEPS:
[sampath@localhost ipc]$. vi smf.c
[sampath@localhost ipc]$. cc smf.c
[sampath@localhost ipc]$. ./a.out

Command requires two arguments

AVR & SVR CET

Page 47

UNIX INTERNALS LAB

11. AIM:
Write a program to find whether a file is having read , write , execute permissions and also
check whether the given name is file or directory
PROGRAM
#include<stdio.h>
AVR & SVR CET

Page 48

UNIX INTERNALS LAB


#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<fcntl.h>
#include<strings.h>
#include<unistd.h>
struct stat sbuf1,sbuf2;
int main(int argc, char *argv[])
{
int fd1,fd2;
int i,j;
char *flines1,*flines2;
if(argc<3)
{
printf("Program requires atleast 2 arguments");
exit(0);
}
for(i=1;i<argc-1;i++)
{
fd1=open(argv[i],O_RDONLY);
if(fd1<0) continue;
flines1=malloc(sbuf1.st_size);
fstat(fd1,&sbuf1);
read(fd1,flines1,sbuf1.st_size);
for(j=i+1;j<argc;j++)

AVR & SVR CET

Page 49

UNIX INTERNALS LAB


{
fd2=open(argv[j],O_RDONLY);
if(fd2<0) continue;
fstat(fd2,&sbuf2);
if(sbuf1.st_size==sbuf2.st_size)
{
flines2=malloc(sbuf2.st_size);
read(fd2,flines2,sbuf2.st_size);
close(fd2);
if(bcmp(flines1,flines2,sbuf2.st_size)==0)
unlink(argv[j]);
free(flines2);
}
}
close(fd1);
free(flines1);
}
exit(0);
}
EXECUTION STEPS:
[sampath@localhost ipc]$. vi frd.c
[sampath@localhost ipc]$. cc frd.c
[sampath@localhost ipc]$. ./a.out

Program requires at least 2 arguments

AVR & SVR CET

Page 50

UNIX INTERNALS LAB

12. AIM:
Write a program to create a fork function and to display child process id , parent process id
,root id

AVR & SVR CET

Page 51

UNIX INTERNALS LAB


PROGRAM

#include<stdio.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
int i,pid,value;
sscanf(argv[1],"%d",&value);
for (i=0;i<value-1;i++)
{
pid=fork();
if(pid==0)
{
printf("I'm child My pid=%d\t",getpid());
printf("My parent pid=%d\t ",getppid());
printf("My Groupid=%d\n ",getgid());
}
else
wait();
}
return 0;
}

AVR & SVR CET

Page 52

UNIX INTERNALS LAB

EXECUTION STEPS:
[sampath@localhost ipc]$. vi fork.c
[sampath@localhost ipc]$. cc fork.c
[sampath@localhost ipc]$. ./a.out

I'm child My pid = 63755

AVR & SVR CET

Page 53

UNIX INTERNALS LAB


My parent pid = 63753
My Groupid =63750

AVR & SVR CET

Page 54

Anda mungkin juga menyukai