Linux - Study Mode

[#861] What is the output of this program? #include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<semaphore.h>

int main()
{
sem_t* sem_id

int value

sem_id = sem_open("new_13",O_CREAT,0666,3)

if(sem_id == SEM_FAILED)
perror("sem_open")

sem_wait(sem_id)

sem_wait(sem_id)

sem_wait(sem_id)

sem_wait(sem_id)

sem_post(sem_id)

sem_post(sem_id)

sem_getvalue(sem_id,&value)

printf("%d
",value)

if(sem_close(sem_id) == -1)
perror("sem_close")

return 0

}
Correct Answer

(D) none of the mentioned

[#862] What is the output of this program? #include<stdio.h>
#include<pthread.h>
#include<semaphore.h>

sem_t st

void *fun_t(void *arg)

void *fun_t(void *arg)
{
printf("Linux
")

pthread_exit("Bye")

sem_post(&st)

}
int main()
{
pthread_t pt

void *res_t

if(pthread_create(&pt,NULL,fun_t,NULL) == -1)
perror("pthread_create")

if(sem_init(&st,0,0) != 0)
perror("sem_init")

if(sem_wait(&st) != 0)
perror("sem_wait")

printf("Example
")

if(pthread_join(pt,&res_t) == -1)
perror("pthread_join")

if(sem_destroy(&st) != 0)
perror("sem_destroy")

return 0

}
Correct Answer

(A) this program will print the only string "Linux"

[#863] What is the output of this program? #include<stdio.h>
#include<sys/types.h>
#include<sys/un.h>
#include<sys/socket.h>

int main()
{
struct sockaddr_un add_server, add_client

int fd_server, fd_client

int len

char ch

fd_server = socket(AF_UNIX,SOCK_STREAM,0)

if(fd_server == -1)
perror("socket")

add_server.sun_family = AF_UNIX

strcpy(add_server.sun_path,"demo_sock")

if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
perror("bind")

len = sizeof(add_client)

fd_client = connect(fd_server,(struct sockaddr*)&add_client,&len)

printf("Example
")

return 0

}
Correct Answer

(C) error

[#864] What is the output of second program if we run the demo1 first and after that we run demo2 in the different terminal? /*This is demo1.c*/
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<string.h>

int main()
{
int shm_id

char *addr

struct shmid_ds ds

shm_id = shmget((key_t)1234,10,0666|IPC_CREAT)

if(shm_id == -1){
perror("shmget")

}
addr = (char*)shmat(shm_id,NULL,SHM_RND)

if(addr == (char *)-1){
perror("shmat")

}
strcpy(addr,"Example")

if (shmdt(addr) != 0){
perror("shmdt")

}
sleep(10)

if( shmctl(shm_id,IPC_RMID,0) == -1){
perror("shmctl")

}
return 0

}
/*This is demo2.c*/
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>

int main()
{
int shm_id

char *addr

struct shmid_ds ds

shm_id = shmget((key_t)1234,10,0666|IPC_CREAT)

if(shm_id == -1){
perror("shmget")

}
addr = (char*)shmat(shm_id,NULL,SHM_RND)

if(addr == (char *)-1){
perror("shmat")

}
printf("%s
",addr)

if (shmdt(addr) != 0){
perror("shmdt")

}
return 0

}
Correct Answer

(A) the program will print the string "Example"

[#865] What is the output of this program? #include<stdio.h>
#include<stdlib.h>

int main()
{
int *ptr

ptr = (int *)calloc(1,sizeof(int))

if (ptr != 0)
printf("%d
",*ptr)

return 0

}
Correct Answer

(A) 0