Linux PGM Memory
Linux PGM Memory
MANAGEMENT.
#include<stdio.h>
#include<malloc.h>
#include<unistd.h>
#include<alloca.h>
void func();
int bss_var,data_var=42;
main()
{
char *p,*b,*nb;
printf("Test locations");
printf("address of main %u",main);
printf("address of func() %u",func);
printf("stack locations");
func();
p=(char*) alloca(32);
if(p!=NULL)
{
printf("start of alloca block %u",p);
printf("end of alloca block %u",p+31);
}
printf("Data Locations");
printf("address of data_var %u\n",&data_var);
printf("zero filled locations");
printf("address of bss_var %u\n",&bss_var);
b=sbrk(32);
nb=sbrk(0);
printf("heap locations");
printf("intial end of heap %u \n",b);
printf("new end of heap %u \n",nb);
b=sbrk(-16);
nb=sbrk(0);
printf("final end of heap %u \n",nb);
}
void func()
{
static int level=0;
int stack_var;
if(++level==3)
return;
printf("stack level %d,address of stack_var %u",level,&stack_var);
func();
}
OUTPUT:
OUTPUT:
#include<stdio.h>
#include<unistd.h>
void main()
{
alarm(1000);
printf("looping forever---\n");
while(1);
}
OUTPUT:
#include<stdio.h>
#include<signal.h>
int alarmflag=0;
void alarmhandler();
void main()
{
signal(SIGALRM,alarmhandler);
alarm(5);
printf("looping---\n");
while(alarmflag!=1)
pause();
printf("loop ends due to alarm signal\n");
}
void alarmhandler()
{
printf("an alarm clock signal was received\n");
alarmflag=1;
}
OUTPUT:
#include<stdio.h>
#include<signal.h>
void main()
{
int pid1,pid2;
pid1=fork();
if(pid1==0)
{
while(1)
{
printf("pid1 is alive\n");
sleep(1);
}
}
pid2=fork();
if(pid2==0)
{
while(1)
{
printf("pid2 is alive\n");
sleep(1);
}}
sleep(3);
kill(pid1,SIGSTOP);
sleep(3);
kill(pid1,SIGCONT);
sleep(3);
kill(pid1,SIGINT);
kill(pid2,SIGINT);
}
OUTPUT:
PR
OGRAM: WRITE A PROGRAM IN LINUX TO EXECUTE WAIT()
SYSTEM CALL.
#include<stdio.h>
#include<unistd.h>
void main()
{
int pid, status, childpid;
printf("I am in the parent process and my pid is %d\n", getpid());
pid = fork();
if(pid!=0) // parent code
{
printf("I am in the parent process with pid %d ppid %d \n", getpid(),getppid());
childpid=wait(&status); // wait for the child to terminate
printf("A child with ppid %d terminated with exit code %d \n", childpid,status);
}
else
{
printf("I am the child process with pid %d and ppid %d \n", getpid(), getppid());
exit(42); // child process terminates
}
printf("pid %d terminates \n", getpid()); // parent process terminates
}
OUTPUT:
#include<unistd.h>
void main()
{
int choice;
char*env[]={"/home/test", NULL}, *cmd[]={"-l","-t","-i",NULL};
printf("Menu of exec family \n");
printf("Press 1: execl() \n");
printf("Press 2: execv() \n");
printf("Press 3: execle() \n");
printf("Press 4: execlp() \n");
printf("Press 5: execve() \n");
printf("Press 6: execvp() \n");
printf("Enter your choice \n");
scanf("%d", &choice);
switch(choice)
{
case 1: execl("/bin/ls","-l",NULL);
break;
case 2: execv("/bin/ls",cmd);
break;
case 3: execle("/bin/ls","-l","-l",NULL,env);
break;
case 4: execlp("ls","-i",NULL);
break;
case 5: execve("/bin/ls",cmd,env);
break;
case 6: execvp("ls",cmd);
break;
default: printf("! invalid choice");
}
}
OUTPUT:
CASE 1:
CASE 2:
CASE 3:
CASE 4:
CASE 5:
CASE 6:
MSGSENDER.C
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#define KEY ((key_t) 98765l)
void main()
{
int i,msqid, n;
struct my_msgbuf
{
long mtype;
char mchar[50];
}msg;
if((msqid=msgget(KEY,0666|IPC_CREAT))<0)
perror("can't create message queue");
printf("msqid=%d",msqid);
strcpy(msg.mchar,"this is message 1");
msgsnd(msqid,(void *)&msg,sizeof(msg.mchar), IPC_NOWAIT);
strcpy(msg.mchar,"this is message 2");
msgsnd(msqid,(void *)&msg,sizeof(msg.mchar), IPC_NOWAIT);
}
MSGRECEIVER.C
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#define KEY ((key_t) 98765l)
void main()
{
int i,msqid, n;
struct my_msgbuf
{
long mtype;
char mchar[50];
}msg;
msg.mtype=0;
if((msqid=msgget(KEY,0400))<0)
perror("can't create message queue");
printf("msqid=%d",msqid);
msgrcv(msqid,(void *)&msg,sizeof(msg.mchar),msg.mtype, MSG_NOERROR|
IPC_NOWAIT);
printf("%s",msg.mchar);
}
OUTPUT: