我试图理解如何使我的程序成为守护进程.所以我遇到的一些事情通常,程序执行以下步骤来成为守护进程:
fork( ).在父母中,打电话exit( ).这可以确保原始父级(守护程序的祖父级)满足其子级终止,守护程序的父级不再运行,并且守护程序不是进程组领导者.最后一点是成功完成下一步的要求.
调用setsid( ),为守护进程提供一个新的进程组和会话,两者都将其作为领导者.这也确保了进程没有关联的控制终端(因为进程刚刚创建了一个新会话,并且不会分配一个).
通过将工作目录更改为根目录chdir( ).这样做是因为继承的工作目录可以在文件系统上的任何位置.守护进程倾向于在系统正常运行时间内运行,并且您不希望保持一些随机目录打开,从而阻止管理员卸载包含该目录的文件系统.
关闭所有文件描述符.
/dev/null.#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/fs.h>
int main (void)
{
pid_t pid;
int i;
/* create new process */
pid = fork ( );
if (pid == -1)
return -1;
else if (pid != 0)
exit (EXIT_SUCCESS);
/* create new session and process group */
if (setsid ( ) == -1) …Run Code Online (Sandbox Code Playgroud) 可能重复:
是否有printf转换器以二进制格式打印?
这是我的计划
#include<stdio.h>
int main ()
{
int i,a=2;
i=~a;
printf("a=%d\ni=%d\n",a,i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是
a=2
i=-3
Run Code Online (Sandbox Code Playgroud)
我希望这个以二进制打印.%x,%o和%d是十六进制,八进制和十进制数,但是什么用于在printf中打印二进制文件?
我在互联网上阅读了很多关于'tr'命令用法的教程.但是,我无法理解如何使用shell脚本加密电子邮件地址使用rot13来移动字符.任何人都可以提供链接或示例吗?
我有一个返回地址的函数如下
struct node *create_node(int data)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=NULL;
printf("create node temp->data=%d\n",temp->data);
return temp;
}
Run Code Online (Sandbox Code Playgroud)
struct node是哪里的
struct node {
int data;
struct node *next;
};
Run Code Online (Sandbox Code Playgroud)
如何在printf("")中查看存储在temp中的地址?
更新
如果我检查gdb中的地址,地址将以十六进制数格式显示,即0x602010,其中相同的地址printf("%p",temp)输入不同的数字,这与我在gdb打印命令中看到的不同.
我接受了一次采访,他们问了我这个问题
#include<stdio.h>
int main ()
{
int* const p=NULL;
int const *q=NULL;
p++;
q++;
printf("%d\n",p);
printf("%d\n",q);
}
Run Code Online (Sandbox Code Playgroud)
以上程序将如何表现
a)p将增加4个字节;
和q也将增加4个字节;
b)p将为零
q将指向前面4个字节的存储器;
c)错误将出现在上述程序中
我无法理解这些陈述之间的区别
int* const p=NULL;
int const *q=NULL;
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main(void)
{
int i,j,k;
char st;
printf("enter string\n");
scanf("%s", st);
printf("the entered string is %s\n", st);
}
Run Code Online (Sandbox Code Playgroud)
编译上面的程序给了我一个警告:
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]
palindrom.c:8:1: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
这是我运行时发生的事情:
$ ./a.out
enter string
kiaaa
the entered string is (null)
Run Code Online (Sandbox Code Playgroud)
编辑:
下面是代码(由另一个版本char st;为char *st):
#include <stdio.h>
int main(void)
{
int …Run Code Online (Sandbox Code Playgroud) 看看这个程序
#include<stdio.h>
int main (){
char c='a';
printf("%d %d", sizeof(c),sizeof('a'));
}
Run Code Online (Sandbox Code Playgroud)
输出是1 4
我知道当我们写一个语句char c ='a';
那么如何在1字节(char c)的空间中存储一些4字节(ASCII码)的东西,为什么没有溢出等.
当进程在运行时执行时,我不清楚内存管理
这是一张图 
我不清楚图像中的以下内容:
文章提到了 http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/
虚拟地址空间,在32位模式下始终是4GB的内存地址块.这些虚拟地址按页表映射到物理内存,
同一篇文章也提到了
Linux通过向起始地址添加偏移量来随机化堆栈,内存映射段和堆.不幸的是,32位地址空间非常紧张,几乎没有随机化的空间并妨碍其有效性.
以下程序的输出
#include<stdio.h>
int main(){
int *p[10];
printf("%ld %ld\n",sizeof(*p),sizeof(p));
}
Run Code Online (Sandbox Code Playgroud)
是
8 <--- sizeof(*p) gives size of single element in the array of int *p[10]
80 <--- sizeof(p) gives size of whole array which is 10 * 8 in size.
Run Code Online (Sandbox Code Playgroud)
现在看下面的程序
#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d;
printf("sizeof(array) = %ld \n",sizeof(array));
printf("sizeof(array[0]) = %ld \n",sizeof(array[0]));
printf("sizeof int %ld\n",sizeof(int));
printf("TOTAL_ELEMENTS=%ld \n",TOTAL_ELEMENTS);
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是
sizeof(array) = 28
sizeof(array[0]) …Run Code Online (Sandbox Code Playgroud)