Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

Linux MCQ Set 1

1. This program will allocate the memory of ___ bytes for pointer “ptr”.

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = realloc(0,sizeof(int)*10);
  8.        return 0;
  9.    }

a) 0
b) 10
c) 40
d) none of the mentioned

View Answer

Answer: c [Reason:] If the first argument of realloc() is NULL, then it behaves just like malloc().

2. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6. 	   char *ptr;
  7.        free(ptr);
  8.        return 0	   
  9.    }

a) this program will print nothing after execution
b) segmentation fault
c) Aborted (core dumped)
d) none of the mentioned

View Answer

Answer: c [Reason:] This prgram is trying to free the memory which is not available in the heap segment. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san *** glibc detected *** ./san: free(): invalid pointer: 0x4a77cff4 *** ======= Backtrace: ========= /lib/libc.so.6[0x4a6489f2] ./san[0x80483c9] /lib/libc.so.6(__libc_start_main+0xf3)[0x4a5e96b3] ./san[0x8048321] ======= Memory map: ======== 08048000-08049000 r-xp 00000000 fd:01 394194 /home/aidlo/san 08049000-0804a000 rw-p 00000000 fd:01 394194 /home/aidlo/san 09233000-09254000 rw-p 00000000 00:00 0 [heap] 4a5ab000-4a5cc000 r-xp 00000000 fd:01 785334 /lib/ld-2.14.90.so 4a5cc000-4a5cd000 r–p 00020000 fd:01 785334 /lib/ld-2.14.90.so 4a5cd000-4a5ce000 rw-p 00021000 fd:01 785334 /lib/ld-2.14.90.so 4a5d0000-4a77a000 r-xp 00000000 fd:01 789110 /lib/libc-2.14.90.so 4a77a000-4a77b000 —p 001aa000 fd:01 789110 /lib/libc-2.14.90.so 4a77b000-4a77d000 r–p 001aa000 fd:01 789110 /lib/libc-2.14.90.so 4a77d000-4a77e000 rw-p 001ac000 fd:01 789110 /lib/libc-2.14.90.so 4a77e000-4a781000 rw-p 00000000 00:00 0 4a7e0000-4a7fc000 r-xp 00000000 fd:01 789128 /lib/libgcc_s-4.6.2-20111027.so.1 4a7fc000-4a7fd000 rw-p 0001b000 fd:01 789128 /lib/libgcc_s-4.6.2-20111027.so.1 b7724000-b7725000 rw-p 00000000 00:00 0 b773d000-b773f000 rw-p 00000000 00:00 0 b773f000-b7740000 r-xp 00000000 00:00 0 [vdso] bfc83000-bfca4000 rw-p 00000000 00:00 0 [stack] Aborted (core dumped) [ aidlo]#

3. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    struct st{
  4.        int a;
  5.        char b;
  6.    };
  7.  
  8.    int main()
  9.    {
  10.        struct st *st_ptr;
  11.        st_ptr = malloc(sizeof(struct st));
  12.        printf("%dn",sizeof(struct st));
  13.        return 0;
  14.    }

a) 8
b) 5
c) 0
d) none of the mentioned

View Answer

Answer: 8 [Reason:] Maximum size of the data type is 4 byte(int) in the structure. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 8 [ aidlo]#

4. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6.        char *ptr;
  7.        ptr = (char *)malloc(sizeof(char)*11);
  8.        ptr = "aidlo";
  9.        printf("%sn",*ptr);
  10.        return 0;
  11.    }

a) aidlo
b) segmentation fault
c) syntax error
d) none of the mentioned

View Answer

Answer: b [Reason:] value of a string can not be assign to a pointer. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san Segmentation fault (core dumped) [ aidlo]#

5. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<string.h>                              
  4.  
  5.    int main()
  6.    {
  7.        char *ptr;
  8.        memcpy(ptr,"aidlo",11);
  9.        printf("%sn",ptr);
  10.        return 0;
  11.    }

a) sanfoudry
b) segmentation fault
c) syntax error
d) none of the mentioned

View Answer

Answer: b [Reason:] Memory must be allocated to pointer “ptr”. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san Segmentation fault (core dumped) [ aidlo]#

6. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<string.h>                              
  4.  
  5.    int main()
  6.    {
  7.        char *ptr;
  8.        ptr = (char*)malloc(sizeof(char)*11);
  9.        strcpy(ptr,"aidlo");
  10.        printf("%dn",*ptr);
  11.        return 0;
  12.    }

a) s
b) aidlo
c) 115
d) segmentation fault

View Answer

Answer: c [Reason:] This program will print the equivalent decimal value at location pointed by “ptr”. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 115 [ aidlo]#

7. Which one of the following in true about this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<string.h>                              
  4.  
  5.    int main()
  6.    {
  7.        char *ptr;
  8.        printf("%pn",ptr);
  9.        ptr = (char *)malloc(sizeof(char));
  10.        printf("%pn",ptr);
  11.        return 0;
  12.    }

a) this program will give segmentation fault
b) this program will print two same values
c) this program has some syntax error
d) none of the mentioned

View Answer

Answer: d [Reason:] This program will print two different values. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 0x4a77cff4 0x980c008 [ aidlo]#

8. In this program the two printed memory locations has the difference of ___ bytes.

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        ptr = (int*)malloc(sizeof(int)*2);
  8.        printf("%pn",ptr);
  9.        printf("%pn",ptr+1);
  10.        return 0;
  11.    }

a) 1
b) 4
c) can not be determined
d) none of the mentioned

View Answer

Answer: b [Reason:] Pointer will increment by 4 bytes because it is the types of integer. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 0x9b4e008 0x9b4e00c [ aidlo]#

9. What is the output of this program?

  1.    #include<stdio.h>
  2.    #inlcude<stdlib.h>
  3.  
  4.    int main()
  5.    {
  6.        int *ptr;
  7.        double *ptr;
  8.        printf("%dn",sizeof(ptr));
  9.        return 0;	   
  10.    }

a) 4
b) 8
c) the compiler will give the error
d) segmentaion fault

View Answer

Answer: c [Reason:] Just see the output carefully. Output: [ aidlo]# gcc -o san san.c san.c: In function ‘main’: san.c:8:10: error: conflicting types for ‘ptr’ san.c:7:7: note: previous declaration of ‘ptr’ was here [ aidlo]#

10. What is the output of this program?

  1.     #include<stdio.h>
  2.     #include<stdlib.h>
  3.     #include<string.h>                              
  4.  
  5.     int main()
  6.     {
  7.         int ptr;
  8.         ptr = (int)malloc(sizeof(int)*10);
  9.         return 0;
  10.     }

a) syntax error
b) segmentaion fault
c) run time error
d) none of the mentioned

View Answer

Answer: d [Reason:] The memory has been allocated but we can not access rest of the memory other than 4 bytes. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san [ aidlo]#

Linux MCQ Set 2

1. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<string.h>
  4.    #include<sys/types.h>
  5.    #include<sys/ipc.h>
  6.    #include<sys/msg.h>
  7.  
  8.    struct data_st{
  9.        long int id;
  10.        char buff[11];
  11.    };
  12.    int main()
  13.    {
  14.        int m_id;
  15.        struct data_st data1, data2;
  16.        m_id = msgget((key_t)181,0666|IPC_CREAT);
  17.        if(m_id == -1)
  18.            perror("msgget");
  19.        data1.id = 1;
  20.        strcpy(data1.buff,"Aidlo");
  21.        if(msgsnd(m_id,&data1,11,0) == -1)
  22.            perror("msgsnd");
  23.        if(msgrcv(m_id,&data2,11,0) == -1)
  24.            perror("msgrcv");
  25.        printf("%sn",data2.buff);
  26.        if(msgctl(m_id,IPC_RMID,0) != 0)
  27.            perror("msgctl");
  28.        return 0;
  29.    }

a) this program will print the string “aidlo”
b) this program will give an error
c) this program will give segmentaion fault
d) none of the mentioned

View Answer

Answer: b [Reason:] The fourth argument of the function msgrcv() is missing in this program. Output: [ aidlo]# gcc -o san san.c san.c: In function ‘main’: san.c:24:2: error: too few arguments to function ‘msgrcv’ /usr/include/sys/msg.h:73:16: note: declared here [ aidlo]#

2. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<string.h>
  4.    #include<sys/types.h>
  5.    #include<sys/ipc.h>
  6.    #include<sys/msg.h>
  7.  
  8.    struct data_st{
  9.        long int id;
  10.        char buff[11];
  11.    };
  12.    int main()
  13.    {
  14.        int m_id;
  15.        struct data_st data1, data2;
  16.        m_id = msgget((key_t)181,0666|IPC_CREAT);
  17.        if(m_id == -1)
  18.            perror("msgget");
  19.        data1.id = 1;
  20.        strcpy(data1.buff,"Aidlo");
  21.        if(msgsnd(m_id,&data1,11,0) == -1)
  22.            perror("msgsnd");
  23.        if(msgctl(m_id,IPC_RMID,0) != 0)
  24.            perror("msgctl");
  25.        if(msgrcv(m_id,&data2,11,1,0) == -1)
  26.            perror("msgrcv");
  27.        printf("%sn",data2.buff);
  28.        return 0;
  29.    }

a) this program will print the string “Aidlo”
b) this program will print the garbage value
c) this program will give segmentation fault
d) none of the mentioned

View Answer

Answer: b [Reason:] The message queue has been removed before recieving the message. Hence the program prints the grabage value of the buffer. Output: [ aidlo]# ./san msgrcv: Invalid argument Ѕ � [ aidlo]#

3. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<string.h>
  4.    #include<sys/types.h>
  5.    #include<sys/ipc.h>
  6.    #include<sys/msg.h>
  7.  
  8.    struct data_st{
  9.        long int id;
  10.        char buff[11];
  11.    };
  12.    int main()
  13.    {
  14.        int m_id,ret;
  15.        struct data_st data1, data2;
  16.        m_id = msgget((key_t)181,0666|IPC_CREAT);
  17.        if(m_id == -1)
  18.            perror("msgget");
  19.        data1.id = 1;
  20.        strcpy(data1.buff,"Aidlo");
  21.        ret = msgsnd(m_id,&data1,11,0);
  22.        printf("%dn",ret);
  23.        if(msgrcv(m_id,&data2,11,1,0) == -1)
  24.            perror("msgrcv");
  25.        if(msgctl(m_id,IPC_RMID,0) != 0)
  26.            perror("msgctl");
  27.        return 0;
  28.    }

a) 0
b) 1
c) -1
d) none of the mentioned

View Answer

Answer: a [Reason:] The function msgsnd() returns 0 when there is no error. Ouptut: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 0 [ aidlo]#

4. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<string.h>
  4.    #include<sys/types.h>
  5.    #include<sys/ipc.h>
  6.    #include<sys/msg.h>
  7.  
  8.    struct data_st{
  9.        long int id;
  10.        char buff[11];
  11.    };
  12.    int main()
  13.    {
  14.        int m_id,ret;
  15.        struct data_st data1, data2;
  16.        m_id = msgget((key_t)181,0666|IPC_CREAT);
  17.        if(m_id == -1)
  18.            perror("msgget");
  19.        data1.id = 1;
  20.        strcpy(data1.buff,"Aidlo");
  21.        if(msgsnd(m_id,&data1,11,0) == -1)
  22.            perror("msgsnd");
  23.        ret = msgrcv(m_id,&data2,11,1,0);
  24.        printf("%dn",ret);     
  25.        if(msgctl(m_id,IPC_RMID,0) != 0)
  26.            perror("msgctl");
  27.        return 0;
  28.    }

a) 0
b) -1
c) 1
d) 11

View Answer

Answer: d [Reason:] The function msgrcv() returns the number of bytes actually copied into its second argument. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 11 [ aidlo]#

5. What is the output of this pogram?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<sys/types.h>
  4.    #include<sys/ipc.h>
  5.    #include<sys/sem.h>
  6.  
  7.    static int sem_p(void);
  8.    static int sem_v(void);
  9.    union semun{
  10.        int val;
  11.        struct semid_ds *buf;
  12.        unsigned short array;
  13.    };
  14.    int sem_id;
  15.    struct semid_ds myds;
  16.    struct sembuf mybuf;
  17.    union semun myun;
  18.    static int sem_p(void)
  19.    {
  20.        mybuf.sem_num = 0;
  21.        mybuf.sem_op = -1;
  22.        mybuf.sem_flg = SEM_UNDO;
  23.        semop(sem_id,&mybuf,1);
  24.    }
  25.    static int sem_v(void)
  26.    {
  27.        mybuf.sem_num = 0;
  28.        mybuf.sem_op = 1;
  29.        mybuf.sem_flg = SEM_UNDO;
  30.        semop(sem_id,&mybuf,1);
  31.    }
  32.    int main()
  33.    {
  34.        int wfd, rfd;
  35.        sem_id = semget((key_t)911,1,0666 | IPC_CREAT);
  36.        myun.val = 1;
  37.        semctl(sem_id,0,SETVAL,myun);
  38.        sem_p();
  39.        printf("Aidlon");
  40.        sem_v();
  41.        semctl(sem_id,0,IPC_RMID,myun);
  42.        return 0;
  43.    }

a) this program will print the string “Aidlo”
b) this process will remain block
c) this program will print the string “Aidlo” & process will remain block
d) none of the mentioned

View Answer

Answer: a [Reason:] The function sem_p() will increment the value of semaphore but it will not block the process. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san Aidlo [ aidlo]#

6. What is the output of this pogram?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<sys/types.h>
  4.    #include<sys/ipc.h>
  5.    #include<sys/sem.h>
  6.  
  7.    static int sem_p(void);
  8.    static int sem_v(void);
  9.    union semun{
  10.        int val;
  11.        struct semid_ds *buf;
  12.        unsigned short array;
  13.    };
  14.    int sem_id;
  15.    struct semid_ds myds;
  16.    struct sembuf mybuf;
  17.    union semun myun;
  18.    static int sem_p(void)
  19.    {
  20.        mybuf.sem_num = 0;
  21.        mybuf.sem_op = -1;
  22.        mybuf.sem_flg = SEM_UNDO;
  23.        semop(sem_id,&mybuf,1);
  24.    }
  25.    static int sem_v(void)
  26.    {
  27.        mybuf.sem_num = 0;
  28.        mybuf.sem_op = 1;
  29.        mybuf.sem_flg = SEM_UNDO;
  30.        semop(sem_id,&mybuf,1);
  31.    }
  32.    int main()
  33.    {
  34.        int wfd, rfd;
  35.        sem_id = semget((key_t)911,1,0666 | IPC_CREAT);
  36.        myun.val = 0;
  37.        semctl(sem_id,0,SETVAL,myun);
  38.        sem_p();
  39.        printf("Aidlon");
  40.        sem_v();
  41.        semctl(sem_id,0,IPC_RMID,myun);
  42.        return 0;
  43.    }

a) this program will print the string “Aidlo”
b) this process will remain block
c) this program will print the string “Aidlo” & process will remain block
d) none of the mentioned

View Answer

Answer: b [Reason:] The initial value of semaphore in this program is 0. Hence the function sem_p() will block the process. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san ^Z [24]+ Stopped ./san [ aidlo]#

7. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<sys/types.h>
  4.    #include<sys/ipc.h>
  5.    #include<sys/sem.h>
  6.  
  7.    static int sem_p(void);
  8.    static int sem_v(void);
  9.    union semun{
  10.        int val;
  11.        struct semid_ds *buf;
  12.        unsigned short array;
  13.    };
  14.    int sem_id;
  15.    struct semid_ds myds;
  16.    struct sembuf mybuf;
  17.    union semun myun;
  18.    static int sem_p(void)
  19.    {
  20.        mybuf.sem_num = 0;
  21.        mybuf.sem_op = -1;
  22.        mybuf.sem_flg = SEM_UNDO;
  23.        semop(sem_id,&mybuf,1);
  24.    }
  25.    static int sem_v(void)
  26.    {
  27.        mybuf.sem_num = 0;
  28.        mybuf.sem_op = 1;
  29.        mybuf.sem_flg = SEM_UNDO;
  30.        semop(sem_id,&mybuf,1);
  31.    }
  32.    int main()
  33.    {
  34.        int wfd, rfd;
  35.        sem_id = semget((key_t)911,1,0666 | IPC_CREAT);
  36.        myun.val = 0;
  37.        semctl(sem_id,0,IPC_RMID,myun);
  38.        semctl(sem_id,0,SETVAL,myun);
  39.        sem_p();
  40.        printf("Aidlon");
  41.        sem_v();
  42.        return 0;
  43.    }

a) this process will remain block
b) this program will print the string “Aidlo”
c) this program will print the string “Aidlo” & process will remain block
d) none of the mentioned

View Answer

Answer: b [Reason:] The semaphore has been removed before calling the function sem_p(). Hence the function sem() will not affect the process. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san Aidlo [ aidlo]#

8. What is the output of second program if we run the san1 first and after that we run san2 in the different terminal?

  1.    /*This is san1.c*/
  2.    #include<stdio.h>
  3.    #include<sys/ipc.h>
  4.    #include<sys/shm.h>
  5.    #include<string.h>
  6.  
  7.    int main()
  8.    {
  9.        int shm_id;
  10.        char *addr;
  11.        struct shmid_ds ds;
  12.        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  13.        if(shm_id == -1){
  14.            perror("shmget");
  15.        }
  16.        addr = (char*)shmat(shm_id,NULL,SHM_RND);
  17.        if(addr == (char *)-1){
  18.            perror("shmat");
  19.        }
  20.        strcpy(addr,"Aidlo");
  21.        if (shmdt(addr) != 0){
  22.            perror("shmdt");
  23.        }
  24.        sleep(10);      
  25.        if( shmctl(shm_id,IPC_RMID,0) == -1){
  26.            perror("shmctl");
  27.        }
  28.        return 0;
  29.    }
  30.    /*This is san2.c*/
  31.    #include<stdio.h>
  32.    #include<sys/ipc.h>
  33.    #include<sys/shm.h>
  34.  
  35.    int main()
  36.    {
  37.        int shm_id;
  38.        char *addr;
  39.        struct shmid_ds ds;
  40.        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  41.        if(shm_id == -1){
  42.            perror("shmget");
  43.        }
  44.        addr = (char*)shmat(shm_id,NULL,SHM_RND);
  45.        if(addr == (char *)-1){
  46.            perror("shmat");
  47.        }
  48.        printf("%sn",addr);
  49.        if (shmdt(addr) != 0){
  50.            perror("shmdt");
  51.        }
  52.        return 0;
  53.     }

a) the program will print the string “Aidlo”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned

View Answer

Answer: a [Reason:] The process of san1.c has written the string “Aidlo” in the shared memory and the process of san2.c accessed the string from the shared memory. This is valid only for 10 seconds from the execution of first program san1.c. Output1: [ aidlo]# gcc -o san1 san1.c [ aidlo]# ./san1

Output2: [ aidlo]# gcc -o san2 san2.c [ aidlo]# ./san2 Aidlo [ aidlo]#

9. What is the output of second program if we run the san1 first and after that we run san2 in the different terminal?

  1.    /*This is san1.c*/
  2.    #include<stdio.h>
  3.    #include<sys/ipc.h>
  4.    #include<sys/shm.h>
  5.    #include<string.h>
  6.  
  7.    int main()
  8.    {
  9.        int shm_id;
  10.        char *addr;
  11.        struct shmid_ds ds;
  12.        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  13.        if(shm_id == -1){
  14.            perror("shmget");
  15.        }
  16.        addr = (char*)shmat(shm_id,NULL,SHM_RND);
  17.        if(addr == (char *)-1){
  18.            perror("shmat");
  19.        }
  20.        strcpy(addr,"Aidlo");
  21.        if (shmdt(addr) != 0){
  22.            perror("shmdt");
  23.        }
  24.        if( shmctl(shm_id,IPC_RMID,0) == -1){
  25.            perror("shmctl");
  26.        }
  27.        return 0;
  28.    }
  29.    /*This is san2.c*/
  30.    #include<stdio.h>
  31.    #include<sys/ipc.h>
  32.    #include<sys/shm.h>
  33.  
  34.    int main()
  35.    {
  36.        int shm_id;
  37.        char *addr;
  38.        struct shmid_ds ds;
  39.        shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  40.        if(shm_id == -1){
  41.            perror("shmget");
  42.        }
  43.        addr = (char*)shmat(shm_id,NULL,SHM_RND);
  44.        if(addr == (char *)-1){
  45.            perror("shmat");
  46.        }
  47.        printf("%sn",addr);
  48.        if (shmdt(addr) != 0){
  49.            perror("shmdt");
  50.        }
  51.        return 0;
  52.     }

a) the program will print the string “Aidlo”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned

View Answer

Answer: b [Reason:] The process of san1.c has written the string “Aidlo” in the shared memory and the process of san2.c could not access the string from the shared memory due to delay. Output1: [ aidlo]# gcc -o san1 san1.c [ aidlo]# ./san1

Output2: [ aidlo]# gcc -o san2 san2.c [ aidlo]# ./san2

[ aidlo]#

10. What is the output of second program if we run the san1 first and after that we run san2 in the different terminal?

  1.     /*This is san1.c*/
  2.     #include<stdio.h>
  3.     #include<sys/ipc.h>
  4.     #include<sys/shm.h>
  5.     #include<string.h>
  6.  
  7.     int main()
  8.     {
  9.         int shm_id;
  10.         char *addr;
  11.         struct shmid_ds ds;
  12.         shm_id = shmget((key_t)1234,10,0666|IPC_CREAT);
  13.         if(shm_id == -1){
  14.             perror("shmget");
  15.         }
  16.         addr = (char*)shmat(shm_id,NULL,SHM_RND);
  17.         if(addr == (char *)-1){
  18.             perror("shmat");
  19.         }
  20.         strcpy(addr,"Aidlo");
  21.         if (shmdt(addr) != 0){
  22.             perror("shmdt");
  23.         }
  24.         sleep(10);      
  25.         if( shmctl(shm_id,IPC_RMID,0) == -1){
  26.             perror("shmctl");
  27.         }
  28.         return 0;
  29.     }
  30.     /*This is san2.c*/
  31.     #include<stdio.h>
  32.     #include<sys/ipc.h>
  33.     #include<sys/shm.h>
  34.  
  35.     int main()
  36.     {
  37.         int shm_id;
  38.         char *addr;
  39.         struct shmid_ds ds;
  40.         shm_id = shmget((key_t)111,10,0666|IPC_CREAT);
  41.         if(shm_id == -1){
  42.             perror("shmget");
  43.         } 
  44.         addr = (char*)shmat(shm_id,NULL,SHM_RND);
  45.         if(addr == (char *)-1){
  46.             perror("shmat");
  47.         } 
  48.         printf("%sn",addr);
  49.         if (shmdt(addr) != 0){
  50.             perror("shmdt");
  51.         }
  52.         return 0;
  53.      }

a) the program will print the string “Aidlo”
b) the program will nothing
c) segmentaion fault
d) none of the mentioned

View Answer

Answer: a [Reason:] The process of san1.c has written the string “Aidlo” in the shared memory and the process of san2.c could not access that shared memory because the key is different. Output1: [ aidlo]# gcc -o san1 san1.c [ aidlo]# ./san1

Output2: [ aidlo]# gcc -o san2 san2.c [ aidlo]# ./san2

[ aidlo]#

Linux MCQ Set 3

1. Which one of the following string will print first by this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.  
  4.    void *fun_t(void *arg);
  5.    void *fun_t(void *arg)
  6.    {
  7.        printf("Aidlon");
  8.        pthread_exit("Bye");
  9.    }
  10.    int main()
  11.    {
  12.        pthread_t pt;
  13.        void *res_t;
  14.        if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
  15.            perror("pthread_create");
  16.        printf("Linuxn");
  17.        if(pthread_join(pt,&res_t) != 0)
  18.            perror("pthread_join");
  19.        return 0;
  20.    }

a) Linux
b) Aidlo
c) it can not be predicted
d) none of the mentioned

View Answer

Answer: b [Reason:] It depends upon the scheduler. Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san Aidlo Linux [ threads]#

2. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.  
  4.    void *fun_t(void *arg);
  5.    void *fun_t(void *arg)
  6.    {
  7.        int ret;
  8.        ret = pthread_exit("Bye");
  9.        printf("%dn",ret);
  10.    }
  11.    int main()
  12.    {
  13.        pthread_t pt;
  14.        void *res_t;
  15.        if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
  16.            perror("pthread_create");
  17.        if(pthread_join(pt,&res_t) != 0)
  18.            perror("pthread_join");
  19.        return 0;
  20.    }

a) 0
b) 1
c) -1
d) none of the mentioned

View Answer

Answer: d [Reason:] The function pthread_exit() does not return any value. Hence this program will give an error. Output: [ aidlo]# gcc -o san san.c -lpthread san.c: In function ‘fun_t’: san.c:8:6: error: void value not ignored as it ought to be [ aidlo]#

3. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.  
  4.    void *fun_t(void *arg);
  5.    void *fun_t(void *arg)
  6.    {
  7.        printf("Aidlon");
  8.        pthread_exit("Bye");
  9.    }
  10.    int main()
  11.    { 
  12.        pthread_t pt;
  13.        void *res_t;
  14.        if(pthread_create(&pt,NULL,fun_t,NULL) != 0)                
  15.            perror("pthread_create");
  16.        return 0;
  17.    }

a) this program will print the string “Aidlo”
b) this program will print nothing
c) segmentation fault
d) run time error

View Answer

Answer: b [Reason:] The pthread_join() function waits for the thread to terminate. Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san [ aidlo]#

4. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.  
  4.    void *fun_t(void *arg);
  5.    void *fun_t(void *arg)
  6.    {       
  7.        printf("%dn",a);
  8.        pthread_exit("Bye");
  9.    }
  10.    int main()
  11.    {
  12.        int a;  
  13.        pthread_t pt;
  14.        void *res_t;         
  15.        a = 10;
  16.        if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
  17.            perror("pthread_create");
  18.        if(pthread_join(pt,&res_t) != 0)
  19.            perror("pthread_join");
  20.        return 0;
  21.    }

a) 10
b) 0
c) -1
d) none of the mentioned

View Answer

Answer: d [Reason:] Each thread has its own stack so local variables are not shared among thread. Hence this program will give an error. Output: [ aidlo]# gcc -o san san.c -lpthread san.c: In function ‘fun_t’: san.c:7:16: error: ‘a’ undeclared (first use in this function) san.c:7:16: note: each undeclared identifier is reported only once for each function it appears in [ aidlo]#

5. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.  
  4.    int a;  
  5.    void *fun_t(void *arg);
  6.    void *fun_t(void *arg)
  7.    {       
  8.        printf("%dn",a);
  9.        pthread_exit("Bye");
  10.    }
  11.    int main()
  12.    {
  13.        pthread_t pt;
  14.        void *res_t;         
  15.        a = 10;
  16.        if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
  17.            perror("pthread_create");
  18.        if(pthread_join(pt,&res_t) != 0)
  19.            perror("pthread_join");
  20.        return 0;
  21.    }

a) 10
b) 0
c) -1
d) none of the mentioned

View Answer

Answer: a [Reason:] Thread of the same process shares the global variables. Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san 10 [ aidlo]#

6. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.  
  4.    int a;  
  5.    void *fun_t(void *arg);
  6.    void *fun_t(void *arg)
  7.    {       
  8.        a = 20;
  9.        pthread_exit("Bye");
  10.    }
  11.    int main()
  12.    {
  13.        pthread_t pt;
  14.        void *res_t;
  15.        a = 10;        
  16.        if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
  17.            perror("pthread_create");
  18.        if(pthread_join(pt,&res_t) != 0)
  19.            perror("pthread_join");
  20.        printf("%dn",a);
  21.        return 0;
  22.    }

a) 10
b) 20
c) segmentation fault
d) none of the mentioned

View Answer

Answer: b [Reason:] In this program the value of variable “a” is changed by the thread “fun_t”. Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san 20 [ aidlo]#

7. Which one of the following statement is not true about this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.  
  4.    void *fun_t(void *arg);
  5.    void *fun_t(void *arg)
  6.    {        
  7.        printf("%dn",getpid());
  8.        pthread_exit("Bye");
  9.    }
  10.    int main()
  11.    {
  12.        pthread_t pt;
  13.        void *res_t;
  14.        if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
  15.            perror("pthread_create");
  16.        if(pthread_join(pt,&res_t) != 0)
  17.            perror("pthread_join");
  18.        printf("%dn",getpid());
  19.        return 0;
  20.    }

a) both printf statements will print the same value
b) both printf statements will print the different values
c) this program will print nothing
d) none of the mentioned

View Answer

Answer: a [Reason:] All the threads of the same process have same PID. Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san 12981 12981 [ aidlo]#

8. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.    #include<fcntl.h>
  4.  
  5.    int fd;
  6.    void *fun_t(void *arg);
  7.    void *fun_t(void *arg)
  8.    {       
  9.        char buff[10];
  10.        int count;
  11.        count = read(fd,buff,10);        
  12.        printf("%dn",count);
  13.        pthread_exit("Bye");
  14.    }
  15.    int main()
  16.    {
  17.        pthread_t pt;
  18.        void *res_t;
  19.        fd = open("san.c",O_RDONLY);        
  20.        if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
  21.            perror("pthread_create");
  22.        if(pthread_join(pt,&res_t) != 0)
  23.            perror("pthread_join");
  24.        return 0;
  25.    }

a) 10
b) 0
c) -1
d) segmentation fault

View Answer

Answer: a [Reason:] Open file descritpors can be shares between threads of the same process Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san 10 [ aidlo]#

9. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<pthread.h>
  3.    #include<fcntl.h>
  4.  
  5.    void *fun_t(void *arg);
  6.    void *fun_t(void *arg)
  7.    {
  8.        pthread_exit("Bye");
  9.        printf("Aidlon"); 
  10.    }
  11.    int main()
  12.    {
  13.        pthread_t pt;
  14.        void *res_t;
  15.        if(pthread_create(&pt,NULL,fun_t,NULL) != 0)                
  16.            perror("pthread_create");
  17.        if(pthread_join(pt,&res_t) != 0)
  18.            perror("pthread_join");
  19.        printf("%sn",res_t);
  20.        return 0;
  21.    }

a) Aidlo
b) Bye
c) segementation fault
d) run time error

View Answer

Answer: b [Reason:] None. Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san Bye [ aidlo]#

10. What is the output of this program?

  1.     #include<stdio.h>
  2.     #include<pthread.h>
  3.  
  4.     void *fun_t(void *arg);
  5.     void *fun_t(void *arg)
  6.     {
  7.         sleep(1);
  8.     }
  9.     int main()
  10.     {
  11.         pthread_t pt;
  12.         void *res_t;
  13.         if(pthread_create(&pt,NULL,fun_t,NULL) != 0)
  14.             perror("pthread_create");
  15.         if(pthread_join(pt,&res_t) != 0)
  16.             perror("pthread_join");
  17.         printf("%sn",res_t);
  18.         return 0;
  19.     }

a) this process will pause
b) segmentation fault
c) run time error
d) none of the mentioned

View Answer

Answer: b [Reason:] This program is trying to print the return value of the thread, but pthread_exit() function is not present in the thread. Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san Segmentation fault (core dumped) [ aidlo]#

Linux MCQ Set 4

1. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<semaphore.h>
  5.  
  6.    int main()
  7.    {
  8.        sem_t* sem_id;
  9.        sem_id = sem_open("sem_value",O_CREAT,0666,0);
  10.        if(sem_id == SEM_FAILED)
  11.            perror("sem_open");
  12.        sem_wait(sem_id);
  13.        printf("Aidlon");
  14.        if(sem_close(sem_id) == -1)
  15.            perror("sem_close");    
  16.        return 0;
  17.    }

a) this program will print the string “Aidlo”
b) this process will block
c) segmentaion fault
d) none of the mentioned

View Answer

Answer: b [Reason:] The initial value of semaphore is 0. As we call the sem_wait() in this program, it blocks the process. Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san ^Z [37]+ Stopped ./san [ aidlo]#

2. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<semaphore.h>
  5.  
  6.    int main()
  7.    {
  8.        sem_t* sem_id;
  9.        int value;
  10.        sem_id = sem_open("sem_value",O_CREAT,0666,0);
  11.        if(sem_id == SEM_FAILED)
  12.            perror("sem_open");
  13.        if(sem_getvalue(sem_id,&value) == -1)
  14.            perror("sem_getvalue");
  15.        printf("%dn",value);
  16.        sem_wait(sem_id);
  17.        printf("Aidlon");
  18.        if(sem_close(sem_id) == -1)
  19.            perror("sem_close");
  20.        return 0;
  21.    }

a) 0
b) Aidlo
c) Both 0 and Aidlo
d) None of the mentioned

View Answer

Answer: a [Reason:] The sem_getvalue() is used to get the current value of the semaphore. Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san 0 ^Z [58]+ Stopped ./san [ aidlo]#

3. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<semaphore.h>
  5.  
  6.    int main()
  7.    {
  8.        sem_t* sem_id;
  9.        sem_id = sem_open("sem_value",O_CREAT,0666,0);
  10.        if(sem_id == SEM_FAILED)
  11.            perror("sem_open");
  12.        sem_post(sem_id);
  13.        printf("Aidlon");
  14.        if(sem_close(sem_id) == -1)
  15.            perror("sem_close");
  16.        return 0;
  17.    }

a) this process will block
b) this program will print the string “Aidlo”
c) segmentation fault
d) none of the mentioned

View Answer

Answer: b [Reason:] The initial value of semaphore is 0 but the sem_post() increments the value of semaphore by 1. Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san Aidlo [ aidlo]#

4. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<semaphore.h>
  5.  
  6.    int main()
  7.    {
  8.        sem_t* sem_id;
  9.        sem_id = sem_open("sem_value",O_CREAT,0666,0);
  10.        if(sem_id == SEM_FAILED)
  11.            perror("sem_open");
  12.        if(sem_close(sem_id) == -1)
  13.            perror("sem_close");
  14.        sem_wait(sem_id);
  15.        printf("Aidlon");
  16.        return 0;
  17.    }

a) this process will block
b) this program will print the string “Aidlo”
c) segmentation fault
d) none of the mentioned

View Answer

Answer: c [Reason:] The sem_wait() is trying to use the semaphore when it is already closed. Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san Segmentation fault (core dumped) [ aidlo]#

5. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<semaphore.h>
  5.  
  6.    int main()
  7.    {
  8.        sem_t* sem_id;
  9.        int value;
  10.        sem_id = sem_open("new_13",O_CREAT,0666,3);
  11.        if(sem_id == SEM_FAILED)
  12.            perror("sem_open");
  13.        sem_wait(sem_id);
  14.        sem_wait(sem_id);
  15.        sem_wait(sem_id);
  16.        sem_wait(sem_id);
  17.        sem_post(sem_id);
  18.        sem_post(sem_id);
  19.        sem_getvalue(sem_id,&value);
  20.        printf("%dn",value);
  21.        if(sem_close(sem_id) == -1)
  22.            perror("sem_close");
  23.        return 0;
  24.    }

a) 2
b) 3
c) 0
d) none of the mentioned

View Answer

Answer: d [Reason:] This process will block when the sem_wait() has been called last time in this program. Output: [ aidlo]# gcc -o san san.c -lpthread [ aidlo]# ./san ^Z [64]+ Stopped ./san [ aidlo]#

6. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<sys/mman.h>
  5.  
  6.    int main()
  7.    {
  8.        int s_id;
  9.        s_id = shm_open("shared_mem",O_CREAT|O_RDWR,0666);
  10.        printf("%dn",s_id);
  11.        if(shm_unlink("shared_mem") == -1)
  12.            perror("shm_unlink");
  13.        return 0;
  14.    }

a) -1
b) 1
c) 2
d) 3

View Answer

Answer: d [Reason:] On success the shm_open() returns a nonnegative file descriptor. Output: [ aidlo]# gcc -o san san.c -lrt [ aidlo]# ./san 3 [ aidlo]

7. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<sys/mman.h>
  5.  
  6.    int main()
  7.    {
  8.        int s_id;
  9.        int *ptr;
  10.        s_id = shm_open("shared_mem",O_CREAT|O_RDWR,0666);
  11.        if(s_id == -1)
  12.            perror("shm_open");
  13.        ptr = mmap(NULL,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
  14.        if(ptr == MAP_FAILED);
  15.            perror("mmap");
  16.        if(munmap(ptr,100) == -1)
  17.            perror("munmap");
  18.        if(shm_unlink("shared_mem") == -1)
  19.            perror("shm_unlink");
  20.        return 0;
  21.    }

a) mmap: Success
b) mmap: Failure
c) munmap: Success
d) munmap: Failure

View Answer

Answer: a [Reason:] Memory of the 100 bytes is mapped successfully as shared memory. Output: [ aidlo]# gcc -o san san.c -lrt [ aidlo]# ./san mmap: Success [ aidlo]#

8. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<sys/stat.h>
  4.    #include<sys/mman.h>
  5.  
  6.    int main()
  7.    {
  8.        int s_id;
  9.        int *ptr;
  10.        s_id = shm_open("shared_mem",O_CREAT|O_RDWR,0666);
  11.        if(s_id == -1)
  12.            perror("shm_open");
  13.        ptr = mmap(NULL,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
  14.        if(ptr == MAP_FAILED);
  15.            perror("mmap");
  16.        ptr = mmap(ptr,100,PROT_WRITE,MAP_PRIVATE,s_id,0);
  17.        if(ptr == MAP_FAILED);
  18.            perror("mmap");
  19.        if(munmap(ptr,100) == -1)
  20.            perror("munmap");
  21.        if(shm_unlink("shared_mem") == -1)
  22.            perror("shm_unlink");
  23.        return 0;
  24.    }

a) mmap: Success
mmap: Success
b) mmap: Success
mmap: Failure
c) segmentation fault
d) none of the mentioned

View Answer

Answer: a [Reason:] None. Output: [ aidlo]# gcc -o san san.c -lrt [ aidlo]# ./san mmap: Success mmap: Success [ aidlo]#

9. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<fcntl.h>
  3.    #include<errno.h>
  4.    #include<sys/stat.h>
  5.    #include<sys/mman.h>
  6.  
  7.    int main()
  8.    {
  9.        int s_id;
  10.        s_id = shm_open("shared_mem",O_CREAT|O_RDWR,0666);
  11.        if(s_id != EACCES)
  12.            perror("Permission grantedn");
  13.        return 0;
  14.    }

a) Permission granted
: Success
b) Permission granted
c) segmentation fault
d) none of the mentioned

View Answer

Answer: a [Reason:] The shm_open() returns the error EACCES, when the permission is denied. Output: [ aidlo]# gcc -o san san.c -lrt [ aidlo]# ./san Permission granted : Success [ aidlo]#

10. What is the output of this program?

  1.     #include<stdio.h>
  2.     #include<fcntl.h>
  3.     #include<errno.h>
  4.     #include<sys/stat.h>
  5.     #include<sys/mman.h>
  6.  
  7.     int main()
  8.     {
  9.         int s_id;
  10.         s_id = shm_open("shared_memory",O_TRUNC,0666);
  11.         if(s_id == -1)
  12.             perror("shm_openn");
  13.         return 0;
  14.     }

a) this program will give an error because OTRUNC is not a valid flag
b) this program will give an error
c) this program will give segmentation fault
d) none of the mentioned

View Answer

Answer: b [Reason:] There is no shared memory object “shared_memory” already present in the system. Output: [ aidlo]# gcc -o san san.c -lrt [ aidlo]# ./san shm_open : No such file or directory [ aidlo]#

Linux MCQ Set 5

1. This program will print the

  1.    #include<stdio.h>
  2.    #include<unistd.h>
  3.  
  4.    int main()
  5.    {
  6.        long int value;
  7.        value = sysconf(_SC_CHILD_MAX);
  8.        printf("%ldn",value);
  9.        return 0;
  10.    }

a) maximum number of simultaneous processes per user id
b) maximum number of child processes of the current process
c) minimum number of simultaneous processes per user id
d) none of the mentioned

View Answer

Answer: a [Reason:] None. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 1024 [ aidlo]#

2. This program will print the

  1.    #include<stdio.h>
  2.    #include<unistd.h>
  3.  
  4.    int main()
  5.    {
  6.        long int value;
  7.        value = sysconf(_SC_OPEN_MAX);
  8.        printf("%ldn",value);
  9.        return 0;
  10.    }

a) maximum number of threads in current process
b) maximum number of files that a process can have open at a time
c) segmentation fault
d) none of the mentioned

View Answer

Answer: a [Reason:] None. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 1024 [ aidlo]#

3. This program will print the

  1.    #include<stdio.h>
  2.    #include<unistd.h>
  3.  
  4.    int main()
  5.    {
  6.        long int value;
  7.        value = pathconf("/home/aidlo",_PC_NAME_MAX);
  8.        printf("%ldn",value);
  9.        return 0;
  10.    }

a) maximum numbers of the file that can store in this directory
b) maximum length of a filename in this directory that the process is allowed to create
c) segmentation fault
d) none of the mentioned

View Answer

Answer: b [Reason:] None. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 255 [ aidlo]#

4. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<unistd.h>
  3.    #include<fcntl.h>
  4.  
  5.    int main()
  6.    {
  7.        long int value;
  8.        int fd;
  9.        fd = open("/home/aidlo/san.c",O_RDONLY);
  10.        value = fpathconf(fd,_PC_LINK_MAX);
  11.        printf("%ldn",value);
  12.        return 0;
  13.    }

a) this program will print the maximum number of links to the file “san.c”
b) this program will print nothing
c) this program will give an error
d) none of the mentioned

View Answer

Answer: a [Reason:] None. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 65000 [ aidlo]#

5. This program will print the

  1.    #include<stdio.h>
  2.    #include<sys/time.h>
  3.    #include<sys/resource.h>
  4.  
  5.    int main()
  6.    {
  7.        struct rlimit limit;
  8.        getrlimit(RLIMIT_FSIZE,&limit);
  9.        printf("%lun",limit.rlim_cur);
  10.        printf("%lun",limit.rlim_max);
  11.        return 0;
  12.    }

a) soft limit of the size of the file in bytes that can be created by the process
b) hard limit of the size of the file in bytes that can be created by the process
c) soft 7 hard limit of the size of the file in bytes that can be created by the process
d) none of the mentioned

View Answer

Answer: c [Reason:] The rlim_cur member specifies the soft limit and rlim_max specifies the hard limit of the resource. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 4294967295 4294967295 [ aidlo]#

6. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<sys/time.h>
  3.    #include<sys/resource.h>
  4.  
  5.    int main()
  6.    {
  7.        struct rlimit limit;
  8.        if(getrlimit(RLIMIT_NOFILE,&limit) != 0)
  9.            perror("getrlimit");
  10.        printf("%lun",limit.rlim_max);
  11.        return 0;
  12.    }

a) this program will print the maximum numbers of the file descriptors that can be opened by a process
b) this program will print the maximum numbers of the child processes of the current process
c) this program will give an error because RLIMIT_NOFILE does not exist
d) none of the mentioned

View Answer

Answer: a [Reason:] None. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 4096 [ aidlo]#

7. The hard limit of the file descriptors that can be opened by this process will become

  1.    #include<stdio.h>
  2.    #include<sys/time.h>
  3.    #include<sys/resource.h>
  4.  
  5.    int main()
  6.    {
  7.        struct rlimit limit;
  8.        limit.rlim_cur = 10;
  9.        limit.rlim_max = 20;
  10.        if(setrlimit(RLIMIT_NOFILE,&limit) != 0)
  11.            perror("setrlimit");
  12.        if(getrlimit(RLIMIT_NOFILE,&limit) != 0)
  13.            perror("getrlimit");
  14.        printf("%lun",limit.rlim_cur);
  15.        printf("%lun",limit.rlim_max);
  16.        return 0;
  17.    }

a) 10
b) 20
c) permisssion denied
d) none of the mentioned

View Answer

Answer: b [Reason:] None.

8. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<sys/time.h>
  3.    #include<sys/resource.h>
  4.  
  5.    int main()
  6.    {
  7.        struct rlimit limit;
  8.        limit.rlim_cur = 10;
  9.        if(setrlimit(RLIMIT_NOFILE,&limit) != 0)
  10.            perror("setrlimit");
  11.        return 0;
  12.    }

a) the soft limit of the file decriptors that can be opened by this process will become 10
b) the hard limit of the file decriptors that can be opened by this process will become 10
c) permission denied
d) none of the mentioned

View Answer

Answer: c [Reason:] None. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san setrlimit: Operation not permitted [ aidlo]#

9. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<sys/time.h>
  3.    #include<sys/resource.h>
  4.  
  5.    int main()
  6.    {
  7.        struct rlimit limit;
  8.        if(getrlimit(RLIMIT_CORE,&limit) != 0)
  9.            perror("getrlimit");
  10.        printf("%lun",limit.rlim_max);
  11.        return 0;
  12.    }

a) maximum size of a core file that can be created by this process
b) maximum number of core files that can be created by this process
c) segmentaion fault
d) none of the mentioned

View Answer

Answer: a [Reason:] None. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 4294967295 [ aidlo]#

10. What is the output of this program?

  1.     #include<stdio.h>
  2.     #include<sys/time.h>
  3.     #include<sys/resource.h>
  4.  
  5.     int main()
  6.     {
  7.         struct rlimit limit;
  8.         if(getrlimit(RLIMIT_DATA,&limit) != 0)
  9.             perror("getrlimit");
  10.         printf("%lun",limit.rlim_max);
  11.         return 0;
  12.     }

a) maximum size of data segment of this process in bytes
b) maximum size of total available storage for this process in bytes
c) segmentaion fault
d) none of the mentioned

View Answer

Answer: b [Reason:] None. Output: [ aidlo]# gcc -o san san.c [ aidlo]# ./san 4294967295 [ aidlo]#

.woocommerce-message { background-color: #98C391 !important; }