I don't like to be a killjoy, but this question seems premature (that's why it's hd the "mythology of AI" tag added to it). The kinds of emergent artificial general intelligence depicted in the movies you mention are in science fiction films because they are science fiction. Most AI researchers do not think they are likely to appear anytime soon. The overwhelming majority of researchers think the most likely times for such a system to appear are "More than 50 years [from now]" or "Never". In part, this is because AI researchers thought we were close to such systems for several decades, despite failing to create them. This suggests that making an artificial general intelligence is much harder than we might expect.
Despite AGI being a long show, there's a lot of recent interest in the AI research community in the social impact of our technologies. The study of these systems is called "ethical AI", and this is the path that the research community as a whole has begun to embark on. A promising approach is to model the process by which humans decide to treat each other well, in the hopes of creating programs act according to that process.