-
以下哪种不是进程的状态 ( B )
A 运行态
B 锁定态
C 睡眠态
D 停止态 -
fork()的返回值不可能是(C)
A -1
B 0
C 1
D 大于10000的正整数 -
常用来进行多任务同步的机制是( B )
A 管道
B 信号量
C 信号
D 共享内存 -
下列哪个函数无法传递进程结束时的状态 ( A)
A close
B exit
C _exit
D return -
以下哪种用法可以等待接收进程号为pid的子进程的退出状态(A )
A waitpid(pid, &status, 0)
B waitpid(pid, &status, WNOHANG)
C waitpid(-1, &status, 0)
D waitpid(-1, &status, WNOHANG) -
What kind of IPC has the highest efficiency? (B )
A semaphore
B sharedmemory
C fifo
D message queue -
下列对无名管道描述错误的是 ( C )
A 半双工的通信模式
B 有固定的读端和写端
C 可以使用lseek函数
D 只存在于内存中 -
下列对于有名管道描述错误的是 ( D )
A 可以用于互不相关的进程间
B 通过路径名来打开有名管道
C 在文件系统中可见
D 管道内容保存在磁盘上 -
下列不属于用户进程对信号的响应方式的是 ( B )
A 忽略信号
B 保存信号
C 捕捉信号
D 按缺省方式处理 -
不能被用户进程屏蔽的信号是 ( B )
A SIGINT
B SIGSTOP
C SIGQUIT
D SIGILL -
下列不属于IPC对象的是(A )
A 管道
B 共享内存
C 消息队列
D 信号灯 -
下列不是用户进程的组成部分的是( D )
A 正文段
B 用户数据段
C 系统数据段
D elf段 -
以下哪种方法无法查看进程的信息 ( )
A ps
B 查看/proc目录
C kill
D top -
默认情况下,不会终止进程的信号是 ( D )
A SIGINT
B SIGKILL
C SIGALRM
D SIGCHLD -
如下关于进程的描述不正确的是(D )。
A 进程在退出时会自动关闭自己打开的所有文件
B 进程在退出时会自动关闭自己打开的网络链接
C 进程在退出时会自动销毁自己创建的所有线程
D 进程在退出时会自动销毁自己打开的共享内存 -
程序代码如下,请按执行顺序写出输出结果
int main() { pid_t pid1, pid2; if ((pid1 = fork()) = = 0) { sleep(3); printf(“info1 from child process_1\n”); exit(0); printf(“info2 from child process_1\n”); } else { if ((pid2 = fork()) = = 0) { sleep(1); printf(“info1 from child process_2\n”); exit(0); } else { wait(NULL); wait(NULL); printf(“info1 from parent process\n”); printf(“info2 from parent process”); _exit(0); } } }
答案:
info1 from child process_2
info1 from child process_1
info1 from parent process
-
编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。–迅雷笔试题
#include <stdio.h> #include <pthread.h> #include <semaphore.h> sem_t sem_A, sem_B, sem_C; void *handler_A(void *arg); void *handler_B(void *arg); void *handler_C(void *arg); int main(int argc, const char *argv[]) { pthread_t thread_A, thread_B, thread_C; sem_init(&sem_A, 0, 1); sem_init(&sem_B, 0, 0); sem_init(&sem_C, 0, 0); pthread_create(&thread_A, NULL, handler_A, NULL); pthread_create(&thread_B, NULL, handler_B, NULL); pthread_create(&thread_C, NULL, handler_C, NULL); pthread_join(thread_A, NULL); pthread_join(thread_B, NULL); pthread_join(thread_C, NULL); sem_destroy(&sem_A); sem_destroy(&sem_B); sem_destroy(&sem_C); return 0; } void *handler_A(void *arg) { int i = 10; while (i > 0) { sem_wait(&sem_A); putchar('A'); i--; sem_post(&sem_B); } pthread_exit(NULL); } void *handler_B(void *arg) { int i = 10; while (i > 0) { sem_wait(&sem_B); putchar('B'); i--; sem_post(&sem_C); } pthread_exit(NULL); } void *handler_C(void *arg) { int i = 10; while (i > 0) { sem_wait(&sem_C); putchar('C'); putchar(10); i--; sem_post(&sem_A); } pthread_exit(NULL); }
-
编写程序实现如下功能
reader.c 从argv[1]所指定的文件中读取内容,依次写到管道/home/linux/myfifo中
writer.c 从管道/home/linux/myfifo中读取内容,写到argv[1]所指定的文件中并保存
代码中可省略头文件,/home/linux/myfifo无需创建
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define N 64
int main(int argc, const char *argv[])
{
if (argc < 2)
{
printf("Input Error :%s <src_file>\n", argv[0]);
}
int fd_dest_w, fd_src_r;
fd_src_r = open(argv[1], O_RDONLY);
if (fd_src_r < 0)
{
perror("Src-File Open Fail ");
return -1;
}
fd_dest_w = open("/home/linux/myfifo");
if (fd_dest_w < 0)
{
perror("Dir-File Open Fail");
return -1;
}
char buf[N];
int Read_Write_Bytes = 0;
while ( (Read_Write_Bytes = read(fd_src_r, buf, N)) > 0 )
{
write(fd_dest_w, buf, Read_Write_Bytes);
}
close(fd_src_r);
close(fd_dest_w);
return 0;
}
//writer.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define N 64
int main(int argc, const char *argv[])
{
if (argc < 2)
{
printf("Input Error :%s <src_file>\n", argv[0]);
}
int fd_dest_w, fd_src_r;
fd_src_r = open("/home/linux/myfifo", O_RDONLY);
if (fd_src_r < 0)
{
perror("Src-File Open Fail ");
return -1;
}
fd_dest_w = open(argv[1], O_WRONLY);
if (fd_dest_w < 0)
{
perror("Dir-File Open Fail");
return -1;
}
char buf[N];
int Read_Write_Bytes = 0;
while ( (Read_Write_Bytes = read(fd_src_r, buf, N)) > 0 )
{
write(fd_dest_w, buf, Read_Write_Bytes);
}
close(fd_src_r);
close(fd_dest_w);
return 0;
}