Porting an application with fork() to pthread_create() - iphone

I am porting a linux application to the iphone and I would like to know how much re-writing I have to do to make it a multi-threaded application rather than a multi-process application.
Also, if I simply replace the forked code with a call to the functions on another thread I get exec_bad_address at seemingly random places in my flow of execution... Does anyone know why this may be the case?
Thanks!

It is exactly the same effort you would undergo in transitioning your application to a multi-threaded one on a unix platform. Simply replacing the forking code with calls to pthread_create() is - almost always - not enough. For instance, you may need to rewrite all of the code that supports inter-process communication. Say one process forks another, and then the two processes communicate through a pipe. Of course, this will not work anymore. Etc. You get the idea.
By the way, while pthreads are available and perfectly working on iOS, you are strongly encouraged to use other technologies available in iOS. For additional details, please read
Concurrency Programming Guide
in particular the Section called "Migrating Away from Threads".
For details about threading, see
Threading Programming Guide

Related

Does swift contain something like threads?

I'm new to Swift, coming from C styled syntax languages (C#, java..). I've been given a past exam paper asking questions about Swift. I've came across one question;
A developer is building an iOS application that needs to obtain data over a network
connection. She is aware that it may take some time to process, but does not want the
application user interface to become unresponsive whilst the task executes. Outline the
steps she could take to achieve this, referring to the features available in the language
she might use.
The first thing that comes to mind is to use threads, and run this process on a separate thread, but from my understanding, threads do not exist in Swift. What's the best answer to this question?
Do not confuse language features with system features.
iOS certainly has ways to let you network asynchronously, and in particular to do a time-consuming download in the background. Indeed, by default all networking is asynchronous on a background thread; networking on the main thread would be a bug in your program. [The question you quoted was probably intended to elicit your knowledge of those features, of which it appears you have none.]
But all of that has nothing to do with what language you use to program iOS.

Is there an OS without locking mechanisms (e.g. mutex)?

I've been re-reading my Operating Systems 1 course script and found a curious note that reads: "Someone wrote a whole OS without locking mechanisms!", in the section about semaphore implementations. Now, it's been a few weeks since that and I don't want to flood the professor with e-mails about every little thing I find curious, but I still have to ask:
Is there a (famous/modern) example of an OS written without locking mechanisms, that supports multiple threads?
Keep in mind that he might've been refering to a student-made project, as we have to implement a fully functional kernel by the end of the course. I turned to SO because Google wasn't much of a help, turning up only some exotic implementations of semaphores/mutexes.

Should I learn how web frameworks work before I use them?

I'm interested in creating a basic web application (for learning, but I want to finish within a few months), and I've read that using a web framework can make that task much easier.
After reading about different frameworks online, it seems to me that using frameworks would hide a lot of detail on they work. I fear that if I use a framework, I won't really know how my website is running.
Is it important to understand how frameworks do what they do, or am I worrying too much? (eg. I don't know how the Linux kernel works, or the C compiler, etc.)
Even if you don't have a particular interest in web frameworks, I would say it's good to play with a few and then crack them open if only for the exposure to new design patterns and solutions that can be applied anywhere in development. (MVC in particular when talking about most web frameworks)
It is (to some extent) important to understand how frameworks work, but you'll never learn that without using them.
So, start using some framework and you'll get basic understanding of it. And then, if you have interest, you can always dig deeper into it (maybe even submit patches and participate in its development). But not in the opposite order.
Using your analogy, you don't become Linux kernel developer without being Linux user for some time.

Which are the kind of applications/services/components where the Actors model (Scala, Erlang) is best suited for?

Besides the benefits of this model over the shared-memory model, I'm just trying to understand where to apply it for higher levels use-cases.
As to Scala, Actors model fits most of the multi-threaded cases one can think about:
Swing GUI application
Web Applications (see Lift framework)
Application Server in multicore environment:
Batch processing of requests/data
Background tracking tasks
Notifications & Scheduled tasks
Actors model makes design much clearer and greatly simplifies interprocess communication.
OTP Framework : Provides really good framework for network based applications.
Helps in making fault tolerant applications . (process restart using Supervisor's in OTP).
Both Synchronous and Asynchronous modes of communication can be done using gen_server.
Event based callbacks can be used using gen_event.
State machine can be programmed easily using gen_fsm (In case you need to follow some states in your application).
A process crash does not bring the whole application down. Only that particular process crashes.
Functional programming language.
A lot easier to program at binary level.
Garbage collection.
Native compilation option.
Fair amount of good useful modules are available.
Able to make good solid concurrent applications easily.
And lots more.... I really enjoyed working on some applications in erlang , making those in c/c++ would have been very difficult.

Communication between applications written in different languages

I am looking at linking a few applications together (all written in different languages like C#, C++, Python) and I am not sure how to go about it.
What I mean by linking? The system I am working on consists of small programs each responsible for a particular processing task. I need to be able to transfer a data set from one application to another easily (the data set in question is not huge, probably a few megabytes) and I also need some form of way to control the current state of the operation (This is where a client-server model rings a bell)
It seems like sockets or maybe SOAP would be a universal solution but just wanted to get some opinions as to what people think about this subject.
Comments/suggestions will be appreciated, thanks!
I personally take a liking towards ØMQ. It's a library that has a familiar BSD-sockets-like interface for passing messages, but you'll find it implements interesting patterns for distributing tasks.
It sounds like you want to arrange several processes in a pipeline. ØMQ allows you to do that using push and poll sockets. (And afterwards, you'll find it's even possible to scale up across multiple processes and machines with little effort.) Take a look at the guide to get started, and the zmq_socket(3) manpage specifically for how push and pull works.
Bindings are available for all the languages you mention.
As for the contents of the message, ØMQ doesn't concern itself with that, they are just blocks of raw data. You can use any format that suits you, such as JSON, or perhaps Protocol Buffers.
What I'm not sure about is the ‘controlling state’ you mention. Are you interested in, for example, cancelling a job halfway through?
For C# to C# you can use Windows Communication Foundation. You may be able to use it with Python and C++ as well.
You may also want to checkout named pipes.
I would think about moving to a model where you eliminate the issue by having centralized data that all of the applications look at. Keep "one source of the truth" so to speak.
Most outside software has trouble linking against C++ code, due to the name-mangling algorithm it uses for its symbols. For that reason, when interfacing with programs written in other languages, it is often best to declare wrappers to things as extern "C" or inside an extern "C" { block.
I need to be able to transfer a data set from one application to another easily (the data set in question is not huge, probably a few megabytes)
Use the file system.
and I also need some form of way to control the current state of the operation
Again, use the file system. A "current_state.json" file with a JSON serialized object is perfect for multiple languages to work with.
It seems like sockets or maybe SOAP would be a universal solution.
Perhaps. But it's overkill for this kind of thing. Your OS already has all the facilities you need. Just use the file system. It's very simple and very reliable.
There are many ways to do interprocess communication. As you said, sockets may be a universal solution. SOAP, i think, is somewhat an overkill. You may also use mailslots. I wrote C++ application using it a couple of years ago. Named pipes could be also a solution, but if you are coding on Windows, it may be difficult.
In my opinion:
Sockets
Mailslots
Are the best candidates.