In: Computer Science
How to convert Sudo Code to C language?
keyboard_process()
{//insert a string.
//check for shared memory
If(shared memory not full)
{
//sendthe string to the shared memory
//send signal to process;
}
Summary-
In the question, there is a requirement to check for shared memory , which has been already created or existed in some part of the system, which can be accessed by the method shmget() and the insertion of the particular string can be done by the method shmat() and after the shared memory access is done , then the shared memory should be detached using the shmdt() method.
Initially there is a check for shared memory and if that exists, storing of the string is to be done in the shared memory or else if it doesn't exist or the shared memory is full, return condition should be specified along with break. Inter Process Communication through shared memory is a concept where two or more process can access the common memory. And communication is done via this shared memory where changes made by one process can be viewed by another process.
0666 | IPC_CREAT indicates giving read and write access to the particular process.
Code-
#include<stdio.h>
#include<conio.h>
#include<sys/ipc.h>
#include <sys/shm.h>
void main()
{
String st;
int v;
scanf("%s",&st);
v= int shmget("key", 1024, 0666| IPC_CREAT)
if(v==0 || size(v)==maxsize)
{
printf("Memory full");
break;
}
else
{
char *str = (char*) shmat(v,(void*)0,0);
shmdt(st);
}
return 0;
}