EmbeddedSystemsLaboratory
ListofExperiments
1. RegisteranewcommandinCLI(CommandLineInterface)
Open terminal
Type -> man(for help)
Type -> pwd (it will show present working directory/folder)
Type -> vi test.c
To save the file press ESC->shift:->wq (to save & quit)
Type -> ls(list the files present in that directory)
Type -> date (it will show the present date and time)
Type -> cal (it will show the calendar of the month)
Type -> cd .. (will take you to previous directory/folder)
Type -> cd press enter(will bring to present working directory)
Type -> mkdirfilename(creates new folder/ new directory)
Type -> cpfilename1filename2(copies filename1 contents to filename2, here only file like .c, .txt etc)
Type -> ls–a (lists everything including the hidden files)
Type -> rmfilename (removes the give file)
Type -> mvfilename1filename2 ( move the content of filename1 to filename2, here folders or files)
2. CreateanewTask
#include<stdio.h>
#include<stdlib.h>
main()
{
printf(“before\n”);
fork(); //fork means duplicate creation.
printf(“After\n”);
}
output:
before
after
after
A Task is nothing but group of processes. When this program executed it creates another process. It prints two times “after”.
To Synchronize the above program write below code
#include<stdio.h>
#include<stdlib.h>
main()
{
if(fork())
{ //parent
printf(“parent entered…\n”);
wait(0);
printf(“parent exiting\n”);
exit(0);
}
else
{// child
printf(“in child\n”);
sleep(5);
exit(0);
}
}
output:
parent entered…
in child
parent exiting
Waitcall will make the process wait to complete child. If one of the process can be affected by other then we can say the two process are synchronized.
3. Interrupt handling
Signal Name | Signal number | meaning |
SIGINT | 2 | Interrupt the process, A process can ignore this signal |
SIGKILL | 9 | A process cannot ignore this signal |
There are so many signal interrupts and predefined kill signals ask the students to find out from help in linux man pages. Above given are some examples.
Program1:
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
main()
{
if(fork())
{
printf(“parent executing”);
signal(SIGCHLD, ISR);
sleep(10);
printf(“parent exiting\n”);
exit(0); // keep here while(1) and try once or take the statement and try once
}
else
{
printf(“child executing\n”);
sleep(5);
printf(“child exiting\n”);
exit(0);
}
}
//interrupt program
Void ISR(int n)
{
printf(“child exited”);
wait(0); // take off this statement and try it
}
output:
child executing
child exiting
parent executing
child exited
parent exiting
Program 2:
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
main()
{
printf(“11111\n”);
sleep(2);
printf(“22222\n”);
alarm(5);
printf(“33333\n”);
sleep(10);
signal(SIGALRM,ISR);
sleep(10);
}
//interrupt program
void ISR(int n)
{
printf(“signaled…%d\n”,n);
alarm(2);
}
output:
11111
22222
33333
Alarm clock
4. Allocate resource using semaphores
program 1:
//-----semaphore--semget------
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
main(int c,char **v)
{
int index;
int semId;
index=atoi(v[1]); // convert a string to integer
semId=semget(10,3,IPC_CREAT | 0600); //get a semaphore set identifier
printf("semval:%d\n",semctl(semId,index,GETVAL)); //semaphore control operations
}
program 2:
//-----thread
#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/sem.h>
union semun
{
int val;
ushort *array;
}arg;
struct sembuf sem_lock={0,-1,SEM_UNDO};
struct sembuf sem_unlock={0,1,SEM_UNDO};
int data;
key_t key=(key_t)2000;
void *worker(void *arg)
{
int thrId=*(int *)arg;
int semId=semget(key,1,0);
if(semId < 0)
{
return NULL;
}
semop(semId,&sem_lock,1);
printf("Thread: %d acquired resource\n",thrId);
sleep(2);
semop(semId,&sem_unlock,1);
printf("Thread: %d released resource\n",thrId);
}
int main()
{
pthread_tthr[8];
intthrId[]={1,2,3,4,5,6,7,8};
inti=0;
intsemId=semget(key,1,IPC_CREAT | 0644);
if(semId< 0)
{
return1;
}
ushort initValue[]={3,0};
unionsemun init;
init.array=initValue;
intret=semctl(semId,0,SETALL,init);
if(ret== -1)
{
semctl(semId,IPC_RMID,0);
printf("semaphore initialization failed\n");
return1;
}
printf("Main thread creating 8 worker threads\n");
for(i=0;i<8;i++)
{
if(pthread_create(&thr[i],NULL,worker,&thrId[i]))
{
semctl(semId,IPC_RMID,0);
printf("Error creating worker thread\n");
return1;
}
}
for(i=0;i<8;i++)
{
pthread_join(thr[i],NULL);
}
semctl(semId,IPC_RMID,0);
return 0;
}
to compile use cc filename -lpthread
output:
Main thread creating 8 worker threads
Thread: 7 acquired resource
Thread: 6 acquired resource
Thread: 8 acquired resource
Thread: 8 released resource
Thread: 6 released resource
Thread: 7 released resource
Thread: 5 acquired resource
Thread: 4 acquired resource
Thread: 3 acquired resource
Thread: 5 released resource
Thread: 4 released resource
Thread: 3 released resource
Thread: 2 acquired resource
Thread: 1 acquired resource
Thread: 2 released resource
Thread: 1 released resource
5. Share resource using MUTEX
//-----mutex-------
#include"headers.h"
int a=10,b=30;
pthread_mutex_t m1,m2;
pthread_t tid1,tid2;
void *thread1_code(void *p)
{
int s;
printf("in thread1...\n");
//pthread_mutex_lock(&m1);
a=sum(a,b);
//pthread_mutex_unlock(&m1);
printf("1:%d\n",a);
pthread_exit(0);
}
void *thread2_code(void *p)
{
int s;
printf("in thread2...\n");
pthread_mutex_lock(&m2);
a=mul(a,b);
printf("2:%d\n",a);
pthread_mutex_unlock(&m2);
pthread_exit(0);
}
main()
{
//int a=10,b=30;
pthread_create(&tid1,NULL,&thread1_code,NULL);
pthread_create(&tid2,NULL,&thread2_code,NULL);
while(1);
}
sum(int x,int y)
{
int result;
result=x+y;
return result;
}
mul(int x,int y)
{
int result;
result=x*y;
return result;
}
output:
in thread2...
2:300
in thread1...
1:330
8. Reader’s Writer’s Problem for concurrent Tasks
program 1:
//---reader writer problem using semaphore--------
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include<sys/sem.h>
#include<sys/ipc.h>
#include <stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include<fcntl.h>
void* function1(void* val);
void* function2(void* val);
// shared values
volatile int X;
volatile int Y;
// declare semaphores
sem_t s1;
sem_t s2;
main()
{
void* status;
pthread_t thread1;
pthread_t thread2;
srand(time(NULL));
// initialize semaphores to zero
sem_init(&s1, 0, 0);
sem_init(&s2, 0, 0);
pthread_create(&thread1, NULL, function1, NULL);
pthread_create(&thread2, NULL, function2, NULL);
pthread_join(thread1, &status);
pthread_join(thread2, &status);
sem_destroy(&s1);
sem_destroy(&s2);
}
void* function1(void* val)
{
while(1)
{
X = rand()%1000; // write
printf("After thread ID A writes to X, X = %d\n", X);
sem_post(&s1); // signal
sem_wait(&s2); // wait
printf("After thread ID A reads from Y, Y = %d\n", Y); // read
sleep(3);
}
}
void* function2(void* val)
{
while(1)
{
sem_wait(&s1); // wait
printf("After thread ID B reads from X, X = %d\n", X); // read
Y = rand()%1000; // write
printf("After thread ID B write to Y, Y = %d\n", Y);
sem_post(&s2); // signal
sleep(3);
}
}
to compile type-> cc filename –lpthread
to execute type-> ./a.out
output:
After thread ID A writes to X, X = 83
After thread ID B reads from X, X = 83
After thread ID B write to Y, Y = 181
After thread ID A reads from Y, Y = 181
After thread ID A writes to X, X = 11
After thread ID B reads from X, X = 11
After thread ID B write to Y, Y = 613
After thread ID A reads from Y, Y = 613
After thread ID A writes to X, X = 358
After thread ID B reads from X, X = 358
After thread ID B write to Y, Y = 792
After thread ID A reads from Y, Y = 792
After thread ID A writes to X, X = 795
After thread ID B reads from X, X = 795
Embedded Systems Lab M.Tech
Reviewed by Suresh Bojja
on
8/26/2015 04:04:00 AM
Rating:
