关于pid_t的理解

pid_t pid = fork();

这里的pid_t类似一个类型,就像int型一样,int型定义的变量都是整型的,pid_t定义的类型都是进程号类型。这个语句的意思是定义了一个pid_t类型的变量pid,fork()函数返回一个进程号,这个进程号赋给了pid。pid_t在头文件types.h(sys/types.h)中定义、

其中fork( void )为创建子进程,有趣的是他有三种不同的返回值

1、在父进程中,fork返回新创建的子进程的PID

2.、在子进程中,fork返回0

3、如果出现错误,fork返回一个负值

另外在子进程被创建之前,只有一个进程在运行,但在fork创建子进程后,两进程同时运行

程序列子:

int main(int argc ,char *argv )

{

// printf("PID =%d \n",getpid());

// printf("PPID =%d \n",getppid());

    pid_t pid;         //定义一个进程号类型的变量 pid

                             // 在此程序中,此时只有一个进程在运行      

   pid=fork();       // 在此程序中,此时只有两个进程在运行   

   if(pid<0)  

          printf("error in fork!");

   else if (pid==0)

           printf("I am the child process,ID is %d\n",getpid());

   else

            printf("I am the parent process,ID is %d\n",getpid());

    return 0;

}

运行结果:创建了一个子进程,输出的结果为 

               I am the parent process,ID is 13655

               I am the child process, ID is 13525,不同次执行时,进程的ID一般是不同的。

 

pid_t与int又有什么区别呢?

pid_t是typedef定义的类型,表示进程的id

在sys/types.h中定义:

typedef short   pid_t;

所以说pid_t就是一个short类型的变量,实际表示的是内核中进程表的索引。

  • 14
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是改动并优化后的代码: ```matlab T_min = 20; % 最小温度 T_max = 40; % 最大温度 T_set = 25; % 设定温度 Kp = 1.0; % 比例系数 Kd = 0.2; % 微分系数 % 创建 PID 控制器对象 pid_ctrl = pid(Kp, 0, Kd); pid_ctrl.Ts = 0.1; % 采样时间 pid_ctrl.InputName = 'error'; pid_ctrl.OutputName = 'u'; pid_ctrl.InputUnit = '℃'; pid_ctrl.OutputUnit = 'V'; % 创建神经网络模型 net = feedforwardnet([10 5]); net = configure(net, rand(1,10), rand(1,1)); net.trainParam.showWindow = false; net.inputs{1}.name = 'error'; net.outputs{2}.name = 'u'; net.inputs{1}.processFcns = {'mapminmax'}; net.outputs{2}.processFcns = {'mapminmax'}; % 初始化数据 t = 0:0.1:100; input_signal = T_min + (T_max - T_min) * rand(size(t)); dt = 0.1; current_temperature = T_min; pid_output = 0; bp_output = 0; % 创建 BP 神经网络控制器对象 bp_controller = fitnet(10); bp_controller.trainParam.showWindow = false; % 绘制温度曲线图 figure; xlabel('Time (s)'); ylabel('Temperature (℃)'); hold on; % 开始控制循环 for i = 1:length(t) % 计算 PID 控制器输出 error = T_set - current_temperature; pid_output = pid_ctrl(error); % 计算神经网络控制器输出 nn_input = error; nn_output = net(nn_input); % 计算 BP 神经网络控制器输出 bp_input = current_temperature; bp_output = bp_controller(bp_input); % 计算总输出 output = pid_output + nn_output + bp_output; % 更新当前温度 current_temperature = current_temperature + output * dt; % 绘制温度曲线 plot(t(i), current_temperature, 'r.'); end % 显示控制结果 disp(['Final temperature: ' num2str(current_temperature) ' ℃']); ``` 改动和优化如下: 1. 将 PID 控制器的计算结果和神经网络控制器的计算结果合并到总输出中,以提高温度控制的准确性。 2. 修改了 BP 神经网络控制器的输入,使其接收当前温度作为输入,以提高控制效果。 3. 去掉了不需要的变量和计算,以简化代码并提高运行效率。 4. 增加了注释,方便理解代码的功能和实现过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值