C 使用shmat后无法在标准输出上打印

C 使用shmat后无法在标准输出上打印,c,unix,output,shared-memory,fgets,C,Unix,Output,Shared Memory,Fgets,所以在这段代码中,puts不能显示输出 如果我删除fgets行,它会打印lola,但是如果我尝试在shm上读写,什么都不会发生。我怎样才能解决这个问题 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/ipc.h> #include <sys/sem.h> #include <sys/types.h> #define SHMSZ

所以在这段代码中,puts不能显示输出

如果我删除fgets行,它会打印lola,但是如果我尝试在shm上读写,什么都不会发生。我怎样才能解决这个问题

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/types.h>

#define SHMSZ 4096

int
main()
{
  pid_t pid1, pid2, pid3;
  pid1 = fork();
  if (pid1 == 0)
    {
      /* child1 */
      int shmid;
      key_t key;
      char * shm;
      key = 5678;
      if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0)
        {
          perror("shmget");
          exit(1);
        }
      if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
        {
          perror("shmat");
          exit(1);
        }
      printf("alright");
      if (fgets(shm,60,stdin))
        {
          /* This doesn't print. */
          puts(shm);
        }
      else
        {
          printf("hurara");
        }
      printf("lola");
    }
  else
    {
      pid2 = fork();
      if(pid2 == 0)
        {
          /* child2 */
        }
      else
        {
          pid3 = fork();
          if (pid3 == 0)
            {
              /* child3 */
            }
          else
            {
              /* parent */
              wait(0);
              wait(0);
              wait(0);
            }   
        }
    }
  return 0;
}

你的包裹有些问题。你编译时有警告吗?如果我使用这些带注释的问题,程序将编译干净,并按照我假设的预期运行

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
/* #include <sys/sem.h> */    /* this one is not needed... */
#include <sys/shm.h>          /* ...but this one is */
#include <sys/types.h>
#include <sys/wait.h>         /* was missing, needed for wait() */
#include <unistd.h>           /* was missing, needed for fork() */

它不像其他字符串指针吗?这就是我在这里使用FGET的原因。你能给我一些建议吗。我需要将字符串数据存储在共享内存中,以便其他进程可以访问该数据。@5gon12eder噢,我将其误读为fgetc@umläute你能告诉我错误吗?我猜SHMSZ>=60?好吧,那我就没主意了。如果您升级您的问题以提供我可以编译和运行的问题,我可以尝试提供更多调试帮助。但从手头的信息来看,坦白说,我不知道。我并不是这样编译的。谢谢。