小编Jai*_*ach的帖子

c共享内存附加/分离/解除分配

我对使用POSIX系统调用的c中的共享内存分段有疑问.我是从客户端和服务器分离和删除段是否正确,或者我只需要从服务器中删除?

考虑我有2个程序

一个用于服务器,一个用于客户端

the steps for the server

1)create memory segment
2)attach
3)detach
4)remove

steps for the client

1)create
2)attach
3)detach
4)remove
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

//server

#include<stdlib.h>
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/shm.h>

#define SHMSZ 100
int main()
{

key_t key;
char c;
int shmid;
char *shm;

key=1025;

//locate
if((shmid=shmget(key,SHMSZ,0666 | IPC_CREAT))<0)
{
perror("shmget");
exit(-1);
}



//attach
if((shm=shmat(shmid,NULL,0))==(char*)-1)
{
perror("shmat");
exit(-1);
}


sprintf(shm,"Hi there");

//shm="Hi There";

while(*shm!='*');
sleep(1);

//detach
shmctl(shmid,IPC_RMID,NULL);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是客户端

//client

#include<stdlib.h>
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/shm.h>

#define SHMSZ …
Run Code Online (Sandbox Code Playgroud)

c client posix shared-memory

4
推荐指数
1
解决办法
4017
查看次数

bss 和数据段中的整型变量大小

我正在使用一个测试程序来理解内核版本为 2.6.32-279.el6.x86_64 的 linux 6.3 上的 C 内存模型。

首先我编译了下面的代码,

#include <stdio.h>
int main(void)
{
    static int i = 100; /* Initialized static variable stored in DS*/
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在运行 size 命令时,我得到了以下结果,

[root@rachitjain jan14]# size a.out
   text    data     bss     dec     hex filename
   1040     488      16    1544     608 a.out
Run Code Online (Sandbox Code Playgroud)

然后,在删除静态变量“i”的初始化后,我的代码变为,

include <stdio.h>
int main(void)
{
    static int i ;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面编译后的运行大小,

[root@rachitjain jan14]# size a.out
   text    data     bss     dec     hex filename
   1040     484      24    1548     60c a.out
Run Code Online (Sandbox Code Playgroud)

bss 部分增加了 …

c linux operating-system memory-management

4
推荐指数
1
解决办法
1745
查看次数

scanf("%d",((*a + i)+ j))和scanf("%d",&a [i] [j])之间的差异

根据我的理解,[i] [j]可以像*(*a + i)+ j一样读取,但我用其中两种表示法扫描扫描,我看到差异,下面是代码片段,

for (i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
    //      scanf("%d",((*a+i)+j));
            scanf("%d",&a[i][j]);
            printf("Address of &a[%d][%d]=%ld , Adress of ((*a+%d)+%d)=%ld\n",i,j,&a[i][j] ,i,j, ((*a+i)+j));
        }
    }
Run Code Online (Sandbox Code Playgroud)

我低于输出,

1
Address of &a[0][0]=140736658532752 , Adress of ((*a+0)+0)=140736658532752
2
Address of &a[0][1]=140736658532756 , Adress of ((*a+0)+1)=140736658532756
3
Address of &a[0][2]=140736658532760 , Adress of ((*a+0)+2)=140736658532760
4
Address of &a[1][0]=140736658532772 , Adress of ((*a+1)+0)=140736658532756
5
Address of &a[1][1]=140736658532776 , Adress of ((*a+1)+1)=140736658532760
6
Address of &a[1][2]=140736658532780 , Adress of ((*a+1)+2)=140736658532764
7
Address of &a[2][0]=140736658532792 , Adress of …
Run Code Online (Sandbox Code Playgroud)

c arrays

2
推荐指数
1
解决办法
738
查看次数