user1.c
/* Our first program is to be run in one terminal. After the headers the shared memory segment
(the size of our shared memory structure) is created with a call to shmget,
with the IPC_CREAT bit specified. */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include "shm_com.h"
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
int shmid;
char buffer[BUFSIZ];
srand((unsigned int)getpid());
shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
/* We now make the shared memory accessible to the program. */
shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %X\n", (int)shared_memory);
/* The next portion of the program assigns the shared_memory segment to shared_stuff,
which then prints out any text in written_by_user2. The loop continues until end is found
in written_by_user1. The call to sleep forces the consumer to sit in its critical section,
which makes the producer wait. */
shared_stuff = (struct shared_use_st *)shared_memory;
shared_stuff->written_by_user2 = 0;
while(running)
{
if (!(shared_stuff->written_by_user2))
{
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->user_1, buffer, TEXT_SZ);
shared_stuff->written_by_user1 = 1;
}
if (shared_stuff->written_by_user2)
{
printf("user2 wrote: %s", shared_stuff->user_2);
sleep( rand() % 4 ); /* make the other process wait for us ! */
shared_stuff->written_by_user2 = 0;
if (strncmp(shared_stuff->user_1, "end", 3) == 0)
{
running = 0;
}
}
if (strncmp(buffer, "end", 3) == 0)
{
running = 0;
}
}
/* Lastly, the shared memory is detached and then deleted. */
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "shmctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
user2.c
/* The second program is the producer and allows us to enter data for consumers.
It's very similar to shm1.c and looks like this. */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include "shm_com.h"
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
char buffer[BUFSIZ];
int shmid;
shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %X\n", (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
shared_stuff->written_by_user1 = 0;
while(running) {
if (!(shared_stuff->written_by_user1))
{
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->user_2, buffer, TEXT_SZ);
shared_stuff->written_by_user2 = 1;
}
if (shared_stuff->written_by_user1)
{
printf("user1 wrote: %s", shared_stuff->user_1);
sleep( rand() % 4 ); /* make the other process wait for us ! */
shared_stuff->written_by_user1 = 0;
if (strncmp(shared_stuff->user_2, "end", 3) == 0)
{
running = 0;
}
}
if (strncmp(buffer, "end", 3) == 0)
{
running = 0;
}
}
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
shm_com.h
/* A common header file to describe the shared memory we wish to pass about. */
#define TEXT_SZ 208
struct shared_use_st {
int written_by_user1;
int written_by_user2;
char user_1[TEXT_SZ];
char user_2[TEXT_SZ];
};
/* Our first program is to be run in one terminal. After the headers the shared memory segment
(the size of our shared memory structure) is created with a call to shmget,
with the IPC_CREAT bit specified. */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include "shm_com.h"
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
int shmid;
char buffer[BUFSIZ];
srand((unsigned int)getpid());
shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
/* We now make the shared memory accessible to the program. */
shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %X\n", (int)shared_memory);
/* The next portion of the program assigns the shared_memory segment to shared_stuff,
which then prints out any text in written_by_user2. The loop continues until end is found
in written_by_user1. The call to sleep forces the consumer to sit in its critical section,
which makes the producer wait. */
shared_stuff = (struct shared_use_st *)shared_memory;
shared_stuff->written_by_user2 = 0;
while(running)
{
if (!(shared_stuff->written_by_user2))
{
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->user_1, buffer, TEXT_SZ);
shared_stuff->written_by_user1 = 1;
}
if (shared_stuff->written_by_user2)
{
printf("user2 wrote: %s", shared_stuff->user_2);
sleep( rand() % 4 ); /* make the other process wait for us ! */
shared_stuff->written_by_user2 = 0;
if (strncmp(shared_stuff->user_1, "end", 3) == 0)
{
running = 0;
}
}
if (strncmp(buffer, "end", 3) == 0)
{
running = 0;
}
}
/* Lastly, the shared memory is detached and then deleted. */
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
fprintf(stderr, "shmctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
user2.c
/* The second program is the producer and allows us to enter data for consumers.
It's very similar to shm1.c and looks like this. */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include "shm_com.h"
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_st *shared_stuff;
char buffer[BUFSIZ];
int shmid;
shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1)
{
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memory attached at %X\n", (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
shared_stuff->written_by_user1 = 0;
while(running) {
if (!(shared_stuff->written_by_user1))
{
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->user_2, buffer, TEXT_SZ);
shared_stuff->written_by_user2 = 1;
}
if (shared_stuff->written_by_user1)
{
printf("user1 wrote: %s", shared_stuff->user_1);
sleep( rand() % 4 ); /* make the other process wait for us ! */
shared_stuff->written_by_user1 = 0;
if (strncmp(shared_stuff->user_2, "end", 3) == 0)
{
running = 0;
}
}
if (strncmp(buffer, "end", 3) == 0)
{
running = 0;
}
}
if (shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
shm_com.h
/* A common header file to describe the shared memory we wish to pass about. */
#define TEXT_SZ 208
struct shared_use_st {
int written_by_user1;
int written_by_user2;
char user_1[TEXT_SZ];
char user_2[TEXT_SZ];
};




![[Android_boot_1.png]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHoRL-nq-eK3g7-wMBtLhg0vp60xjzjGGN2ABDex_RCB7mpt4BqmIXYhD2zjFvtT56visTLN3L0SXRHKVLe4pmVZetkDXLmtFG96YGHA8JnVId-AmddIqnoyZsI20waksI83uYqpz4nhyM/s1600/Android_boot_1.png)
![[Android_boot_2.png]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjriW0z3eulWqe63yaVmWRcjlpw5z1DjCvRoXnK2vqfH3TW-2sRC21kbjmXCWo3HZrR9ZiF-RdzQlmREA5HGvb9JfQ1ch7DUnZIIYmoiKIMlsglH4Z7KrT7cZbelJN3pRstPMpj38pg0A5y/s1600/Android_boot_2.png)
![[Android_boot_3.png]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDL-5qf7nZ8mJZr_yaAGHXD62vzt1IUcGlD31TpT0vobwkQXGEDpKYyPLTex1hmpe0t8vT4KWvaW3CgrCujpMhtyGPyJTNEW32oWvU_DkegcVbePt5ZtViYPUaeSKuWXQqwyLWPZMRXQ0s/s1600/Android_boot_3.png)
![[Android_boot_4.png]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQ2T85__ao5c40wElw9MLplCbr7yqBt_lFXM7IHZfhdW5aTb-0sgxA6VHaoKVLF3B8HkEMbiQVc8p-jlVO64Oje-0UDA4w4KvoDet6sM68p_TFxYS19F6SYTRhBA7acDZ75Vs-E-KCls2i/s1600/Android_boot_4.png)
![[Android_boot_5.png]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVki-dD7Qj-n5hpkJ7z9tol2O3WOfGnQ6vHadKXGNmJn7UjnOTwyOWAiCkQziIYU7OxImAT4I8ifAJlPhDjTtoX8uX4NrI3FHjVZG8A1UHCXIsZVza3cQEAG7-ZBWc0Mbgp87jLFgf09r4/s1600/Android_boot_5.png)
![[Android_boot_6.png]](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTnObhfZoz9SaYdZCCVPdEpR5WLgwOLYBFBuxoka5FnprY4Lfymhj86zP0_d1iWQ3Y2rDPcyhRrM6tUYPZBIW_X3U_x6ToPEwTHJPRSOHh1KaR_J7RSLbqD2zIr5t4sAiz8rA6qyKJpUgB/s1600/Android_boot_6.png)