Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer the following questions: Consider the given code: 1. Rename the shared memory partition /brs_memory to your username_memory. 2. Figure out how to compile

Please answer the following questions:

Consider the given code:

1. Rename the shared memory partition /brs_memory to your username_memory.

2. Figure out how to compile this code.

3. Execute this code and explain what is it doing.

4. What are the command line parameters doing in this code? Code:

int *mem;

int main ( int argc, char **argv ) { int i, pid, kids, shmfd;

kids = argc > 1 ? atoi(argv[1]) : 5; shmfd = shm_open ( "/brs_memory", O_RDWR | O_CREAT, 0666 ); if ( shmfd < 0 ) { fprintf(stderr,"Could not create brs_memory "); exit(1); } ftruncate ( shmfd, kids*sizeof(int) ); mem = (int *)mmap ( NULL, kids*sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0 ); if ( mem == NULL ) { fprintf(stderr,"Could not map brs_memory "); exit(1); } close ( shmfd ); shm_unlink ( "/brs_memory" );

for ( i = 0; i < kids; i++ ) mem[i] = i+1; for ( i = 0; i < kids; i++ ) { pid = fork(); if ( pid < 0 ) { fprintf(stderr,"fork failed at %d ",i); exit(1); } else if ( pid > 0 ) { printf("parent: new child is %d ",pid); } else { usleep ( 1000 ); printf("child %d, parent is %d ",i, getppid()); mem[i] = mem[i] * mem[i]; exit(0); } } for ( i = 0; i < kids; i++ ) wait(NULL); for ( i = 0; i < kids; i++ ) printf("mem[%d] = %d ",i,mem[i]); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions