I want to list all processes in FreeBSD and I have this code below, which uses kvm, but it does not know what KVM_NO_FILES is, and I can't figure how to fix it. If there is another way of doing it, please do share.
#include <stdio.h>
#include <kvm.h>
#include <limits.h>
#include <sys/param.h>
#include <sys/sysctl.h>
int
main(void)
{
char errbuf[_POSIX2_LINE_MAX];
kvm_t *kernel = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
int nentries = 0;
struct kinfo_proc *kinfo = kvm_getprocs(kernel, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &nentries);
int i;
for (i = 0; i < nentries; ++i) {
printf("%s\n", kinfo[i].p_comm);
}
return 0;
}
And I get this error:
root#freebsd:- # cc -lkvm main.c
main.c:11:53: error: use of undeclared identifier 'KVM_NO_FILES'
kvm_t *kernel = kvm_openfiles(NULL. NULL. NULL, HVM_NO_FILES, errbuf):
main.c:13:71: error: invalid application of 'sizeof' to an incomplete type
'struct kinfo_proc'
...= kvm_getprocs(kernel, HERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &nen...
/usr/include/kvM.h:72:8: note: forward declaration of 'struct kinfo_proc'
struct kinfo_proc;
main.c:18:29: error: subscript of pointer to incomplete type 'struct kinfo_proc'
printf("Xs\n", kinfolil.p_comm):
/usr/include/kvM.h:72:8: note: forward declaration of 'struct kinfo_proc'
struct kinfo_proc;
3 errors generated.
Related
Summary
Error and code are at bottom of the question.
I was writing a simple program because I was curious what the size of pointers were and if they differed when they pointed to different data types.
I declared the variables, why are they saying they are undeclared?
Also, for some reason there is no error with the int* but only the bool* and char* as shown in the error message below.
Code
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int* ptri = NULL;
char* ptrc = NULL;
bool* ptrb = NULL;
printf("%lu %lu %lu", sizeof(ptri), sizeof(ptrc), sizeof(ptrb));
}
Error Message
:!clang test.c && ./a.out
test.c:7:5: error: use of undeclared identifier 'bool'
bool* ptrb = NULL;
^
test.c:7:11: error: use of undeclared identifier 'ptrb'
bool* ptrb = NULL;
^
test.c:8:62: error: use of undeclared identifier 'ptrb'
printf("%lu %lu %lu", sizeof(ptri), sizeof(ptrc), sizeof(ptrb));
^
3 errors generated.
shell returned 1
Declare #include <stdbool.h> into the header . It will work.Thanks.
C originally did not have native support for boolean values.
In order to get the things working, you need to import a header file name <stdbool.h>
#include <stdio.h>
#include <stdbool.h>
int main(void) {
int* ptri = NULL;
char* ptrc = NULL;
bool* ptrb = NULL;
printf("%lu %lu %lu", sizeof(ptri), sizeof(ptrc), sizeof(ptrb));
}
The variables are fine (or would be if their declaration were not blocked by other errors).
You have a problem with the type identifier bool. It is not known by (old) standard C.
If you are used to using bool as a type please find out where that type is coming from in your successful other code.
You should write this header " #include <stdbool.h>" which contains type bool
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(void) {
int* ptri = NULL;
char* ptrc = NULL;
bool* ptrb = NULL;
printf("%lu %lu %lu", sizeof(int), sizeof(char), sizeof(bool));
}
This question already has answers here:
C programming: Dereferencing pointer to incomplete type error
(6 answers)
Closed 3 years ago.
I have the problem described in the headline above. These are my files and their code:
run.c:
[...] // I think it's not relevant for the problem
declarations.h:
#ifndef DECLARATIONS_H
#define DECLARATIONS_H
#include <stdint.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <errno.h>
[...]
struct
{
int position;
int currentNumberOfMessages;
int numberOfProcesses;
char buf[MAX_PAYLOAD_LENGTH * MAX_SLOTS];
} mySharedMemory_sct = {0, 0, 0, '0'};
struct mySharedMemory_sct *myShMem_ptr;
[...]
#endif //DECLARATIONS_H
lib.h:
#ifndef LIB_H
#define LIB_H
#include "declarations.h"
#include <stdint.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <errno.h>
[...]
int init (int *argc, char **argv[])
{
/**
* map the shared memory into the process
*/
if ((myShMem_ptr = mmap(NULL, sizeof(mySharedMemory_sct), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
MAP_FAILED)
{
printf("Error: %s\n", strerror(errno));
return EXIT_FAILURE;
}
/**
* increment the number of running processes called by the 'run'-process
*/
myShMem_ptr->numberOfProcesses += 1; <------- ERROR
[...]
return EXIT_SUCCESS;
}
[...]
#endif //LIB_H
For the line marked 'error' the compiler throws this error message:
"dereferencing pointer to incomplete type ‘struct mySharedMemory_sct’"
and highlights the '->' in "myShMem_ptr->numberOfProcesses += 1;" red as the problem.
I've read the other posts to this error message, but the problem causes were different (i think), so I haven't found a solution yet.
In advance: Thank you for your help!
You need to change
struct
{
int position;
int currentNumberOfMessages;
int numberOfProcesses;
char buf[MAX_PAYLOAD_LENGTH * MAX_SLOTS];
} mySharedMemory_sct = {0, 0, 0, '0'};
to
struct mySharedMemory_sct
{
int position;
int currentNumberOfMessages;
int numberOfProcesses;
char buf[MAX_PAYLOAD_LENGTH * MAX_SLOTS];
} mySharedMemory_sct = {0, 0, 0, '0'};
Let's have a look at a simpler case:
struct a {
int x;
} b;
So what do we have here? We have declared a struct and given it the name a, so this makes it possible to declare instances of that struct with struct a <name>. What about b? Well, that is an example of such an instance.
So what does this mean?
struct a {
int x;
} b = {0};
Well, it does certainly NOT mean that when you create an instance of struct a that the instance will have its x value initialized to 0. It only means that this is true for the very instance b.
You have not posted the complete code, but I suspect that this might do what you want:
struct mySharedMemory_sct {
int position;
int currentNumberOfMessages;
int numberOfProcesses;
char buf[MAX_PAYLOAD_LENGTH * MAX_SLOTS];
} mySharedMemory_sct = {0, 0, 0, '0'};
struct mySharedMemory_sct *myShMem_ptr = &mySharedMemory_sct;
An important thing to remember here is that mySharedMemory_sct and struct mySharedMemory_sct are two completely different things. mySharedMemory_sct is a variable with type struct mySharedMemory_sct. You can change their names independently of each other.
So I have had this problem where I keep getting error codes only in my main that (1) The struct has already been defined when I keep my struct in a header file and (2) I am using incompatible pointer types.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "functDefs.h"
#include "writeToFile.c"
#include "readFile.c"
#include "inputContactInfo.c"
#include "contactInfoStruct.h"
int main(void) {
int i = 0;
char *ynAns;
struct contactId *contactInfo;
contactInfo = malloc(sizeof(struct contactId));
do {
if(ynAns != NULL) {
free(ynAns);
}
ynAns = malloc(sizeof(char) * 5);
printf("\nDo you wish to enter a new contact (Yes or No)?: ");
fgets(ynAns, 5, stdin);
ynAns[(strlen(ynAns) - 1)] = '\0';
if (strcmp(ynAns, "Yes") == 0) {
printf("\n");
contactInfo = realloc(contactInfo, sizeof(struct contactId) * (i + 1));
contactInfo[i] = inputContactInfo();
i++;
}
} while(strcmp(ynAns, "No") != 0);
writeToFile(contactInfo, i);
readFile(i);
free(contactInfo);
return 0;
}
Then here are my function definitions:
void writeToFile(struct contactId *contInfo, int numContacts);
struct contactId *inputContactInfo();
void readFile(int numContacts);
And this is the struct header file:
struct contactId {
char firstName[20];
char lastName[20];
char companyName[50];
char phoneNumber[15];
char email[50];
};
I get errors like:
IOlist.c: In function ‘main’:
IOlist.c:28:40: error: incompatible types when assigning to type ‘struct contactId’ from type ‘struct contactId *’
contactInfo[i] = inputContactInfo();
^
IOlist.c:34:21: warning: passing argument 1 of ‘writeToFile’ from incompatible pointer type
writeToFile(contactInfo, i);
^
In file included from IOlist.c:5:0:
writeToFile.c:7:6: note: expected ‘struct contactId *’ but argument is of type ‘struct contactId *’
void writeToFile(struct contactId *contInfo, int numContacts) {
^
And these errors as well:
In file included from IOlist.c:5:0:
writeToFile.c:7:6: error: conflicting types for ‘writeToFile’
void writeToFile(struct contactId *contInfo, int numContacts) {
^
In file included from IOlist.c:4:0:
functDefs.h:1:6: note: previous declaration of ‘writeToFile’ was here
void writeToFile(struct contactId *contInfo, int numContacts);
^
In file included from readFile.c:4:0,
from IOlist.c:6:
contactStruct.h:1:8: error: redefinition of ‘struct contact’
struct contact {
^
In file included from writeToFile.c:4:0,
from IOlist.c:5:
contactStruct.h:1:8: note: originally defined here
struct contact {
^
Your function inputContactInfo() returns a pointer to struct. But the place where it tries to returnn pointer is a struct. You need declare struct contactId **contactInfo, allocate memory for each element, and then you can correctly assign your pointer to contactInfo[i].
I don't know what i'm doing wrong ..
Do you have any ideas what i'm doing wrong? Structure was declared in header file sender.h - code below
After trying to compile this program I got this error:
Sender/Sender.c: In function 'SenderCreate':
Sender/Sender.c:50: error: 'Sender' has no member named 'sim_buf'
Sender/Sender.c:51: error: 'Sender' has no member named 'sim_buf_length'
Sender/Sender.c: In function 'SenderExecuteTask':
Sender/Sender.c:75: error: 'sim_buf_length' undeclared (first use in this function)
Sender/Sender.c:75: error: (Each undeclared identifier is reported only once
Sender/Sender.c:75: error: for each function it appears in.)
Sender/Sender.c:77: error: 'sim_buf' undeclared (first use in this function)
make: *** [Sender.o] Error 1
Code of program below:
#include <stdlib.h> // calloc, free
#include <stdio.h>
#include "Sender.h"
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include "../../rdo_module/readout_dev.h"
struct Sender_s {
ET4DataSource *data_source;
unsigned int num_of_images;
unsigned int num_of_el;
ConditionWait *condition_wait;
int sock;
struct sockaddr *PC;
char *sim_buf;
char *sim_buf_length;
};
void SenderSetNumOfEl(Sender *self, unsigned int num_of_el) {
self->num_of_el = num_of_el;
}
Sender *
SenderCreate(ET4DataSource *data_source,
unsigned int num_of_images,
unsigned int num_of_el,
ConditionWait *condition_wait,
int sock,
struct sockaddr *PC,
char *sim_buf, // here I get some problems
int *sim_buf_length) // and here
{
#ifdef DEBUG
printf("Sender.c SenderCreate line 25: Sender *self = calloc(1, sizeof(Sender));\n");
#endif
Sender *self = calloc(1, sizeof(Sender));
#ifdef DEBUG
printf("Sender.c SenderCreate line 25: success\n");
#endif
self->data_source = data_source;
self->num_of_images = num_of_images;
self->num_of_el = num_of_el;
self->condition_wait = condition_wait;
self->sock = sock;
self->PC = PC;
self->sim_buf = sim_buf;
self->sim_buf_length = sim_buf_length;
return self;
}
void
SenderDestroy(Sender *self)
{
free(self);
}
void *
SenderExecuteTask(void *self_)
{
Sender *self = self_;
ET4Buffer *buf = NULL;
int n = 0;
int c_len = sizeof(*(self->PC));
while(1) {
if(*sim_buf_length) {
n=sendto(self->sock, sim_buf, *sim_buf_length, 0, self->PC, c_len);
if(n < 0) {
perror("error in sendto()");
return NULL;
}
}
return NULL;
}
Code of sender.h below:
#ifndef __SENDER_H__
#define __SENDER_H__
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include "../Utils/ConditionWait.h"
#include "../DataSource/ET4DataSource.h"
typedef struct Sender_s Sender;
Sender *
SenderCreate(ET4DataSource *data_source,
unsigned int num_of_images,
unsigned int num_of_el,
ConditionWait *condition_wait,
int sock,
struct sockaddr *PC,
char *sim_buf,
int *sim_buf_length);
void
SenderDestroy(Sender*self);
void *
SenderExecuteTask(void *self_);
void SenderSetNumOfEl(Sender *self, unsigned int num_of_el);
#endif /*__SENDER_MAKER_H__*/
The error messages and the source in the question don't match!
When I take the source given, the compiler tells, what is wrong.
There is no member sim_buf_length, (note it does not complain about sim_buf_length_flag)
I don't get the error message "error: ‘Sender’ has no member named ‘sim_buf’", because the member is clearly present
Furthermore the types char* (Sender_s member) and int* (SenderCreate argument) don't match
The error messages for function SenderExecuteTask are clear, there are no variables declared sim_buf or sim_buf_length(_flag). Probably the function signature should have been
void *SenderExecuteTask(Sender *self);
and then in the definition self->sim_buf and self->sim_buf_length(_flag) used respectively.
I started a week ago understanding and working with semaphores and shared memory, and actually created this program; the problem is I can't find anything wrong with it. I've been looking at it for hours and everything seems correct. The code compiles and i can create the build but when I execute it nothing happens.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <sys/fcntl.h>
#include <semaphore.h>
#define MAXCHILDS 4
#define MAX_SIZE 10
#define MAX_WRITES 4
typedef struct{
int m[MAX_SIZE][MAX_SIZE];
} matrix;
/*fork variables*/
pid_t child[MAXCHILDS];
/*semphores variables */
sem_t *empty, *full, * mutex;
/*share memory id*/
int shmid;
/*shared memory array pointer */
matrix * sh_mem;
/*pointer to matrix*/
int **p;
void init(){
/*create pointer to matrix*/
p = &sh_mem->m;
/*semaphores unlink and creation */
sem_unlink("EMPTY");
empty=sem_open("EMPTY",O_CREAT|O_EXCL,0700,MAX_WRITES);
sem_unlink("FULL");
full=sem_open("FULL",O_CREAT|O_EXCL,0700,0);
sem_unlink("MUTEX");
mutex=sem_open("MUTEX",O_CREAT|O_EXCL,0700,1);
/*initialize shared memory */
shmid = shmget(IPC_PRIVATE,sizeof(matrix),IPC_CREAT|0777);
/*map shared memory*/
sh_mem = (matrix*)shmat(shmid,NULL,0);
if(sh_mem== (matrix*)(-1)){
perror("shmat");
}
}
void writer(int ** m){
int i,k;
for(i = 0;i<MAX_SIZE;i++){
for(k= 0;k<MAX_SIZE;k++){
m[i][k] = 0;
}
}
}
void reader(int **m){
int i = 0;
int k = 0;
for(i = 0;i<MAX_SIZE;i++){
for(k= 0;k<MAX_SIZE;k++){
printf(m[i][k]);
}
printf("\n");
}
}
void terminate() {
sem_close(empty);
sem_close(full);
sem_close(mutex);
sem_unlink("EMPTY");
sem_unlink("FULL");
sem_unlink("MUTEX");
shmctl(shmid, IPC_RMID, NULL);
}
int main(int argc, char **argv)
{
int i;
init();
for(i = 0;i<MAXCHILDS;i++){
if ((child[i] = fork()) < 0) // error occured
{
perror("Fork Failed");
exit(1);
}
if ((child[i] = fork())==0){
writer(sh_mem->m);
exit(0);
}
}
/*father*/
reader(sh_mem->m);
wait(NULL);
terminate();
return 0;
}
The children are supposed to write the the matrix in shared memory, and the father is supposed to read the shared memory array and the print the matrix.
Can you help me with this? Thanks for the help ...
The primary error here is that reader and writer take a different type of argument than you're passing to them, as gcc -Wall points out:
test.c: In function ‘main’:
test.c:92:13: warning: passing argument 1 of ‘writer’ from incompatible pointer type [enabled by default]
test.c:49:6: note: expected ‘int **’ but argument is of type ‘int (*)[10]’
test.c:97:5: warning: passing argument 1 of ‘reader’ from incompatible pointer type [enabled by default]
test.c:58:6: note: expected ‘int **’ but argument is of type ‘int (*)[10]’
As provided, the program segfaulted in the parent and every child. When I changed the parameter type of reader and writer from int **m to int m[MAX_SIZE][MAX_SIZE] (along with the fixes below), the program ran successfully, as far as I can tell.
There are a number of other errors:
You need to #include <sys/wait.h>.
The global int **p isn't used and its initialization has the same type error as the reader and writer functions did.
The printf call in reader needs a format string; I used "%d ".
As Jonathan Leffler pointed out, you need to call fork() only once each time through the loop in main.
All but the last of those were highlighted by compiler warnings.
In studying why this program was failing, I also used strace -f to identify which syscalls and processes were actually busted. The semaphore-related syscalls, for example, appear to be returning successfully--although as Jonathan pointed out, you should check their return values for errors, because failing as early as possible makes it much easier to debug problems.