我在高级Linux编程中遇到了一个概念.这是一个链接:参考4.5 GNU/Linux线程实现.
我很清楚作者所说的概念,但我对他为线程打印processID所解释的程序感到困惑.
这是代码
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void* thread_function (void* arg)
{
fprintf (stderr, "child thread pid is %d\n", (int) getpid ());
/* Spin forever. */
while (1);
return NULL;
}
int main ()
{
pthread_t thread;
fprintf (stderr, "main thread pid is %d\n", (int) getpid ());
pthread_create (&thread, NULL, &thread_function, NULL);
/* Spin forever. */
while (1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据作者的上述代码的输出是
% cc thread-pid.c -o thread-pid -lpthread
% ./thread-pid & …Run Code Online (Sandbox Code Playgroud) 我正在我的主程序中执行fork,并在子进程中执行exec,这将运行另一个程序.现在我想终止子进程(即exec调用的程序)并返回主程序(或父程序).我怎么能实现这一点..我尝试用ctrl + c,但它杀死父进程和孩子也.请帮助我.
/*This is main.c*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
void sig_int(void);
void sig_term(void);
pid_t pid,ppid;
int main(char argc,char **argv){
int n;
char ch;
printf("***********Application to start or stop services**********\n");
do
{
printf("Enter 1 to start service no.1\n");
printf("Enter 2 to start service no.2\n");
printf("Enter 3 to start service no.3\n");
scanf("%d",&n);
if(fork() == 0)
{
switch(n)
{
case 1: printf("starting service no. 1..\n");
printf("checking whether the given service is already running...\n");
// …Run Code Online (Sandbox Code Playgroud) 我在我的代码中使用czmq和zmq库.我SIGINT通过调用signalmain 注册了一个信号处理程序.代码如下所示:
#include "czmq.h"
void sig_int(int signal);
void* pub_handler(){
zctx_t *context = zctx_new ();
void *publisher = zsocket_new (context, ZMQ_PUB);
zsocket_connect (publisher, "tcp://localhost:5555");
sleep(1);
char topic[20] = "REQ: speedlimit";
// while (true)
{
sleep( randof(10) );
zstr_sendm (publisher, topic);
zstr_send (publisher, "driver analysis data");
}
zctx_destroy (&context);
}
void* sub_handler(){
zctx_t *context = zctx_new();
void *subscriber = zsocket_new (context, ZMQ_SUB);
zsocket_connect (subscriber, "tcp://localhost:5557");
srandom ((unsigned) time (NULL));
char subscription [20] = "RESP: …Run Code Online (Sandbox Code Playgroud) 我正在寻找如何在vim编辑器中使用TODO列表.我command Todo noautocmd vimgrep /TODO\|FIXME/j ** | cw在我的.vimrc文件中使用过.我想知道如何调用它来查看TODO列表
任何帮助将不胜感激.
谢谢.
我最近尝试了一些C编程,偶然发现了以下问题.我正在使用带有MinGW 32位的NetBeans 7.4 64 IDE.这是一个简短的示例代码,它突出了我的问题:
int main(void) {
unsigned short int temp;
char *pointer;
pointer = malloc(12 * sizeof(char));
printf("The pointers value is %d \n", (int)pointer);
printf("Type a short string:\n");
gets(pointer);
printf("The pointers value is %d \n", (int)pointer);
printf("Type an int: \n");
//This line changes the char pointer to an apparently random value
scanf("%d", &temp);
//Segmentation fault upon this point
printf("The pointers value is %d \n", (int)pointer);
//And here as well
free(pointer);
return (EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)
直到scanf一切都很好.读取的字符串被写入指向的内存空间指针.但是,在处理了scanf后,指针的值会发生变化,因此指针指向任何空间.所以不仅我的字符串丢失了,而且在尝试访问/释放不属于我的程序的内存时也会出现分段错误.
价值变化显然是随机的.每次我调试这个程序时,指针都会变为另一个值.
我已经推断出unsigned short int是错误的,或者说是我的scanf中错误的格式说明符(%d而不是%hu).如果我将unsigned …