waitpid头文件(用c语言做以下题目:已有一个已排好的数组,要求输入一个数后,按原来排序的规律将它插入数组中)

本文目录
- 用c语言做以下题目:已有一个已排好的数组,要求输入一个数后,按原来排序的规律将它插入数组中
- 本人初学Linux以及Linux C开发,现在有几个IPC进程间通信的问题请大家帮忙看看,多谢!
- 什么是linux的进程
- c语言怎么创建线程和使用
- 二级C语言:已有一个已排好序的数组,要求输入一个数后,按原来的排序的规律将它插入数组中
- c程序 中wait:什么意思
- 关于父子进程的执行顺序和执行过程
用c语言做以下题目:已有一个已排好的数组,要求输入一个数后,按原来排序的规律将它插入数组中
1、先逐个筛选数组中的每个元素。
2、再比较每相邻的两个元素。
3、根据比较结果进行元素的交换,以实现排序。
4、如果要从终端输入数组,就加一个输入函数。
5、最后,如果是从小到大排序,基本逻辑也是一样的。
注意事项:
C语言是仅产生少量的机器语言以及不需要任何运行环境支持便能运行的高效率程序设计语言。尽管C语言提供了许多低级处理的功能,但仍然保持着跨平台的特性,以一个标准规格写出的C语言程序可在包括类似嵌入式处理器以及超级计算机等作业平台的许多计算机平台上进行编译。
本人初学Linux以及Linux C开发,现在有几个IPC进程间通信的问题请大家帮忙看看,多谢!
1, 应用程序应该使用 #include 《sys/types.h》, bits/types.h 是被 sys/types.h 引用的内部头文件,应用程序不应该直接引用。 linux/types.h 也不要直接引用,它也是内部头文件,用户不要直接用。
2,如果你在创建子进程之前调用任何函数,这些函数都只被父进程调用了。 在创建子进程后,一个函数被哪个进程调用,要看它处在哪个进程的路径上,比如下面代码, test1 和test3均只被父进程调用, test2 被两个进程都调用了。
int pid;
test1();
pid = fork();
if (pid == 0) {
test2();
exit(0);
} else {
test2();
test3()
exit(0);
}
3,一个 shell login对应一个session,所以你开了3个session,一个session对应一个controlling terminal。 controlling terminal 主要用来指定输入输出设备,一个session中可以有任意多个process group,比如你在任意shell下使用以下命令:
sleep 100 &
cmd1 | cmd2 &
tail -f /var/log/messages
此时,这个终端下对应的session里至少有四个(如果之前没有开过其他后台任务话,就是4个)process group,分别是
shell 本身
sleep 100
cmd1 | cmd2
tail -f /var/log/messages
其中 cmd1 | cmd2 这个process group里有两个进程, tail -f /var/log/messages 进程为前台进程
4, 你所说的 status 是否是指父进程中用 wait/waitpid 调用中提供的那个存放子进程状态的指针? 如果是,那么你的说法部分正确,只有当子进程的正常结束(return或者exit),且结束值为0时,status 才等于 0, 其他情况下(比如子进程被信号kill等),不能直接判断 status 的值,应该用以下函数来判断:
WIFEXITED(stat_val)
Evaluates to a non-zero value if status was returned for a child
process that terminated normally.
WEXITSTATUS(stat_val)
If the value of WIFEXITED(stat_val) is non-zero, this macro
evaluates to the low-order 8 bits of the status argument that
the child process passed to _exit() or exit(), or the value the
child process returned from main().
WIFSIGNALED(stat_val)
Evaluates to a non-zero value if status was returned for a child
process that terminated due to the receipt of a signal that was
not caught (see 《signal.h》).
WTERMSIG(stat_val)
If the value of WIFSIGNALED(stat_val) is non-zero, this macro
evaluates to the number of the signal that caused the termina�\
tion of the child process.
WIFSTOPPED(stat_val)
Evaluates to a non-zero value if status was returned for a child
process that is currently stopped.
WSTOPSIG(stat_val)
If the value of WIFSTOPPED(stat_val) is non-zero, this macro
evaluates to the number of the signal that caused the child
process to stop.
WIFCONTINUED(stat_val)
Evaluates to a non-zero value if status was returned for a child
process that has continued from a job control stop.
5, 当然可以。
什么是linux的进程
fork()函数,Linux系统调用
头文件:
#include 《unistd.h》
函数定义:
int fork( void );
返回值:
子进程中返回0,父进程中返回子进程ID,出错返回-1
函数说明:
一个现有进程可以调用fork函数创建一个新进程。由fork创建的新进程被称为子进程(child process)。fork函数被调用一次但返回两次。两次返回的唯一区别是子进程中返回0值而父进程中返回子进程ID。
子进程是父进程的副本,它将获得父进程数据空间、堆、栈等资源的副本。注意,子进程持有的是上述存储空间的“副本”,这意味着父子进程间不共享这些存储空间,它们之间共享的存储空间只有代码段。
‘fork()’函数用于从已存在进程中创建一个新进程。新进程称为子进程,而原进程称为父进程。你可以通过检查‘fork()’函数的返回值知道哪个是父进程,哪个是子进程。父进程得到的返回值是子进程的进程号,而子进程则返回0。以下这个范例程序说明它的基本功能: //------------------------------ example 1 pid_t pid;
switch (pid = fork())
{
case -1:
/* 这里pid为-1,fork函数失败 */
/* 一些可能的原因是 */
/* 进程数或虚拟内存用尽 */
perror(“The fork failed!“);
break;
case 0:
/* pid为0,子进程 */
/* 这里,我们是孩子,要做什么? */
/* ... */
/* 但是做完后, 我们需要做类似下面: */
_exit(0);
default:
/* pid大于0,为父进程得到的子进程号 */
printf(“Child’s pid is %d\n“,pid);
}//------------------------------ example 2#define TRUE 1
while (TRUE){ /* repeat forever */
type_prompt(); /* display prompt on the screen */
read_command(command, parameters); /* read input from terminal */
if (fork() != 0){ /* fork off child process */
/* Parent code. */
waitpid(1, &status, 0); /* wait for child to exit */
} else {
/* Child code. */
execve(command, parameters, 0); /* execute command */
}
}
这些是我再网上帮你找到的资料,看完之后,自己也学到不少,呵呵 ^^
希望它对你有点帮助吧,好好体会
还有一点就是进程的定义:进程是程序在计算机上的一次执行活动。当你运行一个程序,你就启动了一个进程。显然,程序是死的(静态的),进程是活的(动态的)。说白了进程就是正在运行的程序,比如说有程序 p1 和 p2,当程序p1运行是就是进程p1 ,如果正在运行的是p2,那么就是进程p2
c语言怎么创建线程和使用
1、添加线程相关的头文件:#include《pthread.h》
2、线程创建函数是pthread_create()函数,该函数的原型为:
int pthread_create(pthread_t *thread,pthread_attr_t *attr,void* (*start_routine)(void*),void *arg);3、线程退出函数是pthread_exit()函数,该函数的原型为:
void pthread_exit(void *retval);创建线程的示例程序如下:
/***程序说明:创建线程函数pthread_create()函数的使用。
*/
#include 《stdio.h》
#include 《pthread.h》
#include 《unistd.h》
#include 《stdlib.h》
#include 《string.h》
//打印标识符的函数
void print_ids(const char *str)
{
pid_t pid; //进程标识符
pthread_t tid; //线程标识符
pid=getpid(); //获得进程号
tid=pthread_self(); //获得线程号
printf(“%s pid:%u tid:%u (0x%x)\n“,
str,(unsigned int)pid,(unsigned int)tid,(unsigned int)tid); //打印进程号和线程号
}
//线程函数
void* pthread_func(void *arg)
{
print_ids(“new thread:“); //打印新建线程号
return ((void*)0);
}
//主函数
int main()
{
int err;
pthread_t ntid; //线程号
err=pthread_create(&ntid,NULL,pthread_func,NULL); //创建一个线程
if(err != 0)
{
printf(“create thread failed:%s\n“,strerror(err));
exit(-1);
}
print_ids(“main thread:“); //打印主线程号
sleep(2);
return 0;
}
二级C语言:已有一个已排好序的数组,要求输入一个数后,按原来的排序的规律将它插入数组中
#include 《stdio.h》
int main() {
int a = {9,12,13,23,24,35,46,54,57,67,89};
int i,j,x,n = 11;
int flag = 1;
for(i = 0; i 《 n; ++i)
printf(“%d “,a[i]);
printf(“\n要插入的数:“);
scanf(“%d“,&x);
for(i = 0; i 《 n && flag; ++i) {
if(a[i] 》 x) {
for(j = n; j 》 i; --j)
a[j] = a[j - 1];
a[i] = x;
flag = 0;
}
}
if(flag) a[n] = x;
++n;
printf(“删除%d后:\n“,x);
for(i = 0; i 《 n; ++i)
printf(“%d “,a[i]);
printf(“\n“);
return 0;
}
c程序 中wait:什么意思
C语言wait()函数:结束(中断)进程函数(常用)
相关函数:waitpid, fork
头文件:#include 《sys/types.h》 #include 《sys/wait.h》
定义函数:pid_t wait (int * status);
函数说明:wait()会暂时停止目前进程的执行, 直到有信号来到或子进程结束. 如果在调用wait()时子进程已经结束, 则wait()会立即返回子进程结束状态值. 子进程的结束状态值会由参数status 返回, 而子进程的进程识别码也会一快返回. 如果不在意结束状态值, 则参数 status 可以设成NULL. 子进程的结束状态值请参考waitpid().
返回值:如果执行成功则返回子进程识别码(PID), 如果有错误发生则返回-1. 失败原因存于errno 中.
范例
#include 《unistd.h》
#include 《sys/types.h》
#include 《sys/wait.h》
main()
{
pid_t pid;
int status, i;
if(fork() == 0)
{
printf(“This is the child process. pid =%d\n“, getpid());
exit(5);
}
else
{
sleep(1);
printf(“This is the parent process, wait for child...\n“;
pid = wait(&status);
i = WEXITSTATUS(status);
printf(“child’s pid =%d . exit status=^d\n“, pid, i);
}
}
执行:
This is the child process. pid=1501
This is the parent process, wait for child...
child’s pid =1501, exit status =5
关于父子进程的执行顺序和执行过程
(1)fork
函数用于从已存在进程中创建一个新进程。新进程称为子进程,而原进程称为父进
程。这两个分别带回它们各自的返回值,其中父进程的返回值是子进程的进程号,而子进程
则返回
0,大于0则是父进程。因此,可以通过返回值来判定该进程是父进程还是子进程。
使用 fork
函数得到的子进程是父进程的一个复制品,它从父进程处继承了整个进程的地
址空间,包括进程上下文、进程堆栈、内存信息、打开的文件描述符、信号控制设定、进程
优先级、进程组号、当前工作目录、根目录、资源限制、控制终端等,而子进程所独有的只
有它的进程号、资源使用和计时器等。因此可以看出,使用
fork
函数的代价是很大的,它复
制了父进程中的代码段、数据段和堆栈段里的大部分内容,使得
fork
函数的执行速度并不
很快。
(2)所以他们是同时进行的,要想停止父进程或子进程 就用exit()函数退出创建子进程,父进程退出
pid=fork();
if(pid》0){
exit(0);
}
(3)如果不想退出就用wait
函数,它是用于使父进程(也就是调用
wait
的进程)阻塞,直到一个子进程结束或者该进程接到了一个指定的信号为止。如果该父进程没有子进程或者他的子进程已经结束,
wait则就会立即返回。waitpid
的作用和 wait
一样,但它并不一定要等待第一个终止的子进程,它还有若干选项,如可提供一个非阻塞版本的
wait
功能,也能支持作业控制。实际上
wait
函数只waitpid
函数的一个特例,在
Linux 内部实现
wait
函数时直接调用的就是 waitpid
函数。它们的头文件都是#include
《sys/types.h》 #include 《sys/wait.h》

更多文章:
易语言网页api接口怎么调用(易语言,怎么读取网页json的api)
2026年10月11日 08:00
majority of(the majority of 和 a majority of的区别以及用法例句)
2026年10月11日 07:40
another time(another time和other time的区别)
2026年10月11日 05:00








